Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add url.name option to specify the file name #113

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,11 @@ How do we fetch the package source if we have the target version number?
- `fetch.tarball = tarball_url`
- `fetch.docker = owner/name`

Optional `nix-prefetch fetchgit` config, which make sense only when the fetcher equals to `fetch.github` or `fetch.git`.
They can exist simultaneously.
Optional config for `nix-prefetch-url`, applies when the fetcher equals to `fetch.url`.

- `url.name = file_name`

Optional config for `nix-prefetch-git`, applies when the fetcher equals to `fetch.github` or `fetch.git`.

- `git.deepClone`
- `git.fetchSubmodules`
Expand Down
3 changes: 3 additions & 0 deletions app/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ parseConfig raw = runExcept $ case decodeWith tableDecoder raw of
not $ null gk,
"fetch.git" `notElem` keys && "fetch.github" `notElem` keys
]
<>
-- url
[KeyUnexpected pkg uk | let uk = filter ("url.name" ==) keys, not $ null uk, "fetch.url" `notElem` keys]
-- docker
<> [ KeyUnexpected pkg dk
| let dk = filter (T.isPrefixOf "docker.") keys,
Expand Down
8 changes: 4 additions & 4 deletions app/Config/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import Data.Text (Text)
import qualified Data.Text as T
import TOML

githubDecoder :: Decoder (Text, Text)
githubDecoder = makeDecoder $ \case
gitHubNameDecoder :: Decoder (Text, Text)
gitHubNameDecoder = makeDecoder $ \case
v@(String s) -> case T.split (== '/') s of
[owner, repo] -> pure (owner, repo)
_ -> invalidValue "unexpected github format: it should be in the format of [owner]/[repo]" v
v -> typeMismatch v

vscodeExtensionDecoder :: Decoder (Text, Text)
vscodeExtensionDecoder = makeDecoder $ \case
vscodeExtensionNameDecoder :: Decoder (Text, Text)
vscodeExtensionNameDecoder = makeDecoder $ \case
-- assume that we can't have '.' in extension's name
v@(String s) -> case T.split (== '.') s of
[publisher, extName] -> pure (publisher, extName)
Expand Down
9 changes: 5 additions & 4 deletions app/Config/PackageFetcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ _GitOptions _ x = pure x

gitHubDecoder :: Decoder PackageFetcher
gitHubDecoder = do
(owner, repo) <- getFieldsWith githubDecoder ["fetch", "github"]
(owner, repo) <- getFieldsWith gitHubNameDecoder ["fetch", "github"]
gitOptions <- gitOptionsDecoder
pure $ \v -> gitHubFetcher (owner, repo) v & _GitOptions .~ gitOptions

Expand All @@ -104,19 +104,20 @@ pypiDecoder = pypiFetcher <$> getFields ["fetch", "pypi"]
--------------------------------------------------------------------------------

openVsxDecoder :: Decoder PackageFetcher
openVsxDecoder = openVsxFetcher <$> getFieldsWith vscodeExtensionDecoder ["fetch", "openvsx"]
openVsxDecoder = openVsxFetcher <$> getFieldsWith vscodeExtensionNameDecoder ["fetch", "openvsx"]

--------------------------------------------------------------------------------

vscodeMarketplaceDecoder :: Decoder PackageFetcher
vscodeMarketplaceDecoder = vscodeMarketplaceFetcher <$> getFieldsWith vscodeExtensionDecoder ["fetch", "vsmarketplace"]
vscodeMarketplaceDecoder = vscodeMarketplaceFetcher <$> getFieldsWith vscodeExtensionNameDecoder ["fetch", "vsmarketplace"]

--------------------------------------------------------------------------------

urlDecoder :: Decoder PackageFetcher
urlDecoder = do
url <- getFields ["fetch", "url"]
pure $ \(coerce -> v) -> urlFetcher $ T.replace "$ver" v url
name <- getFieldsOpt ["url", "name"]
pure $ \(coerce -> v) -> urlFetcher' (T.replace "$ver" v url) name

--------------------------------------------------------------------------------

Expand Down
8 changes: 4 additions & 4 deletions app/Config/VersionSource.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ listOptionsDecoder =
--------------------------------------------------------------------------------

gitHubReleaseDecoder :: Decoder VersionSource
gitHubReleaseDecoder = uncurry GitHubRelease <$> getFieldWith githubDecoder "github"
gitHubReleaseDecoder = uncurry GitHubRelease <$> getFieldWith gitHubNameDecoder "github"

--------------------------------------------------------------------------------

gitHubTagDecoder :: Decoder VersionSource
gitHubTagDecoder = do
(_owner, _repo) <- getFieldWith githubDecoder "github_tag"
(_owner, _repo) <- getFieldWith gitHubNameDecoder "github_tag"
_listOptions <- listOptionsDecoder
pure GitHubTag {..}

Expand Down Expand Up @@ -142,12 +142,12 @@ httpHeaderDecoder = do
--------------------------------------------------------------------------------

openVsxDecoder :: Decoder VersionSource
openVsxDecoder = uncurry OpenVsx <$> getFieldWith vscodeExtensionDecoder "openvsx"
openVsxDecoder = uncurry OpenVsx <$> getFieldWith vscodeExtensionNameDecoder "openvsx"

--------------------------------------------------------------------------------

vscodeMarketplaceDecoder :: Decoder VersionSource
vscodeMarketplaceDecoder = uncurry VscodeMarketplace <$> getFieldWith vscodeExtensionDecoder "vsmarketplace"
vscodeMarketplaceDecoder = uncurry VscodeMarketplace <$> getFieldWith vscodeExtensionNameDecoder "vsmarketplace"

--------------------------------------------------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions nvfetcher_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ fetch.docker = "library/alpine"
src.git = "https://github.com/githubtraining/example-dependency"
fetch.git = "https://github.com/githubtraining/example-dependency"
git.fetchSubmodules = true

[wallpaper]
fetch.url = "https://files.yande.re/image/3fc51f6a2fb10c96b73dd0fce6ddb9c8/yande.re%201048717%20dress%20garter%20lolita_fashion%20ruo_gan_zhua.jpg"
src.manual = "latest"
# Override the name of the file in the Nix store
url.name = "wallpaper.jpg"
19 changes: 13 additions & 6 deletions src/NvFetcher/NixFetcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module NvFetcher.NixFetcher
gitHubReleaseFetcher',
gitFetcher,
urlFetcher,
urlFetcher',
openVsxFetcher,
vscodeMarketplaceFetcher,
tarballFetcher,
Expand Down Expand Up @@ -79,12 +80,14 @@ sha256ToSri sha256 = do
[x] -> pure $ coerce x
_ -> fail $ "Failed to parse output from nix hash to-sri: " <> T.unpack out

runNixPrefetchUrl :: Text -> Bool -> Action Checksum
runNixPrefetchUrl url unpack = do
runNixPrefetchUrl :: Text -> Bool -> Maybe Text -> Action Checksum
runNixPrefetchUrl url unpack name = do
(CmdTime t, Stdout (T.decodeUtf8 -> out), CmdLine c) <-
quietly $
command [EchoStderr False] "nix-prefetch-url" $
[T.unpack url] <> ["--unpack" | unpack]
[T.unpack url]
<> ["--unpack" | unpack]
<> concat [["--name", T.unpack name] | Just name <- [name]]
putVerbose $ "Finishing running " <> c <> ", took " <> show t <> "s"
case takeWhile (not . T.null) $ reverse $ T.lines out of
[x] -> sha256ToSri x
Expand Down Expand Up @@ -121,13 +124,13 @@ runFetcher = \case
result <-
if useFetchGit
then runNixPrefetchGit [trimming|https://github.com/$_fowner/$_frepo|] (coerce _rev) _fetchSubmodules _deepClone _leaveDotGit
else runNixPrefetchUrl [trimming|https://github.com/$_fowner/$_frepo/archive/$ver.tar.gz|] True
else runNixPrefetchUrl [trimming|https://github.com/$_fowner/$_frepo/archive/$ver.tar.gz|] True mempty
pure FetchGitHub {_sha256 = result, ..}
FetchUrl {..} -> do
result <- runNixPrefetchUrl _furl False
result <- runNixPrefetchUrl _furl False _name
pure FetchUrl {_sha256 = result, ..}
FetchTarball {..} -> do
result <- runNixPrefetchUrl _furl True
result <- runNixPrefetchUrl _furl True mempty
pure FetchTarball {_sha256 = result, ..}
FetchDocker {..} -> do
(CmdTime t, Stdout out, CmdLine c) <-
Expand Down Expand Up @@ -213,6 +216,10 @@ gitHubReleaseFetcher' (owner, repo) f (coerce -> ver) =
urlFetcher :: Text -> NixFetcher Fresh
urlFetcher url = FetchUrl url Nothing ()

-- | Create a fetcher from url specifying the file name
urlFetcher' :: Text -> Maybe Text -> NixFetcher Fresh
urlFetcher' url name = FetchUrl url name ()

-- | Create a fetcher from openvsx
openVsxFetcher ::
-- | publisher and extension name
Expand Down
17 changes: 12 additions & 5 deletions src/NvFetcher/PackageSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module NvFetcher.PackageSet
fetchGit,
fetchGit',
fetchUrl,
fetchUrl',
fetchOpenVsx,
fetchVscodeMarketplace,
fetchTarball,
Expand Down Expand Up @@ -194,23 +195,23 @@ data Prod (r :: [Type]) where
class Member (a :: Type) (r :: [Type]) where
proj :: Prod r -> a

instance {-# OVERLAPPING #-} NotElem x xs => Member x (x ': xs) where
instance {-# OVERLAPPING #-} (NotElem x xs) => Member x (x ': xs) where
proj (Cons x _) = x

instance Member x xs => Member x (_y ': xs) where
instance (Member x xs) => Member x (_y ': xs) where
proj (Cons _ r) = proj r

instance TypeError (ShowType x :<>: 'Text " is undefined") => Member x '[] where
instance (TypeError (ShowType x :<>: 'Text " is undefined")) => Member x '[] where
proj = undefined

-- | Project optional elements from 'Prod'
class OptionalMember (a :: Type) (r :: [Type]) where
projMaybe :: Prod r -> Maybe a

instance {-# OVERLAPPING #-} NotElem x xs => OptionalMember x (x ': xs) where
instance {-# OVERLAPPING #-} (NotElem x xs) => OptionalMember x (x ': xs) where
projMaybe (Cons x _) = Just x

instance OptionalMember x xs => OptionalMember x (_y ': xs) where
instance (OptionalMember x xs) => OptionalMember x (_y ': xs) where
projMaybe (Cons _ r) = projMaybe r

instance OptionalMember x '[] where
Expand Down Expand Up @@ -515,6 +516,12 @@ fetchGit' e (u, f) = fetch e $ f . gitFetcher u
fetchUrl :: Attach PackageFetcher (Version -> Text)
fetchUrl e f = fetch e (urlFetcher . f)

-- | This package is fetched from url
--
-- Args are a function which constructs the url from a version and a file name
fetchUrl' :: Attach PackageFetcher (Text, Version -> Text)
fetchUrl' e (name, f) = fetch e (\v -> FetchUrl (f v) (Just name) ())

-- | This package is fetched from Open VSX
--
-- Args are publisher and extension name
Expand Down
15 changes: 11 additions & 4 deletions test/PrefetchSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ spec = aroundShake $

specifyChan "docker" $
runPrefetchRule' (_sha256 &&& _imageDigest) testDockerFetcher
`shouldReturnJust`
( Checksum "sha256-uaJxeiRm94tWDBTe51/KwUBKR2vj9i4i3rhotsYPxtM=",
ContainerDigest "sha256:65a2763f593ae85fab3b5406dc9e80f744ec5b449f269b699b5efd37a07ad32e"
)
`shouldReturnJust` ( Checksum "sha256-uaJxeiRm94tWDBTe51/KwUBKR2vj9i4i3rhotsYPxtM=",
ContainerDigest "sha256:65a2763f593ae85fab3b5406dc9e80f744ec5b449f269b699b5efd37a07ad32e"
)

specifyChan "url with name" $
runPrefetchRule
( urlFetcher'
"https://files.yande.re/image/3fc51f6a2fb10c96b73dd0fce6ddb9c8/yande.re%201048717%20dress%20garter%20lolita_fashion%20ruo_gan_zhua.jpg"
(Just "foo.jpg")
)
`shouldReturnJust` Checksum "sha256-wkiXDN6vPFtx88krcQ4szK6dJNjtrDxrsNa3ZvHlfMQ="

testDockerFetcher :: NixFetcher Fresh
testDockerFetcher =
Expand Down
Loading