Skip to content

Commit 53a120a

Browse files
authored
Read spago.yaml files and use them to create Manifests (#593)
1 parent 68dddd9 commit 53a120a

File tree

8 files changed

+109
-30
lines changed

8 files changed

+109
-30
lines changed

.github/workflows/main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ jobs:
4343
- name: "Check that all Dhall compiles, and fixtures correctly conform to a Manifest"
4444
run: nix develop --command 'registry-verify-dhall'
4545

46-
- name: "Install dependencies"
47-
run: nix develop --command 'registry-install'
46+
- name: "Build project"
47+
run: nix develop --command 'registry-build'
4848

4949
- name: "Run tests"
5050
run: nix develop --command 'registry-test'

app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"registry-foreign": "file:../foreign",
77
"registry-lib": "file:../lib",
8-
"xhr2": "^0.2.1"
8+
"xhr2": "^0.2.1",
9+
"yaml": "^2.2.1"
910
}
1011
}

app/spago.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ package:
5959
- registry-lib
6060
- run
6161
- safe-coerce
62+
- spago-core
6263
- strings
6364
- sunde
6465
- these

app/src/App/API.purs

+77-22
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ import Run (AFF, EFFECT, Run)
8080
import Run as Run
8181
import Run.Except (EXCEPT)
8282
import Run.Except as Except
83+
import Spago.Core.Config as Spago
84+
import Spago.Core.Prelude as Spago.Prelude
85+
import Spago.Log as Spago.Log
8386
import Sunde as Sunde
8487

8588
-- | Operations can be exercised for old, pre-registry packages, or for packages
@@ -371,31 +374,58 @@ publish source payload = do
371374
, "sources indicated by the `files` key in your manifest."
372375
]
373376

374-
-- If this is a legacy import, then we need to construct a `Manifest` for it.
377+
-- If the package doesn't have a purs.json we can try to make one - possible scenarios:
378+
-- - in case it has a spago.yaml then we know how to read that, and have all the info to move forward
379+
-- - if it's a legacy import then we can try to infer as much info as possible to make a manifest
375380
let packagePursJson = Path.concat [ packageDirectory, "purs.json" ]
376381
hadPursJson <- Run.liftEffect $ FS.Sync.exists packagePursJson
377382
unless hadPursJson do
378-
Notify.notify $ "Package source does not have a purs.json file. Creating one from your bower.json and/or spago.dhall files..."
379-
address <- case existingMetadata.location of
380-
Git _ -> Except.throw "Legacy packages can only come from GitHub."
381-
GitHub { subdir: Just subdir } -> Except.throw $ "Legacy packages cannot use the 'subdir' key, but this package specifies a " <> subdir <> " subdir."
382-
GitHub { owner, repo } -> pure { owner, repo }
383-
384-
version <- case LenientVersion.parse payload.ref of
385-
Left _ -> Except.throw $ "The provided ref " <> payload.ref <> " is not a version of the form X.Y.Z or vX.Y.Z, so it cannot be used."
386-
Right result -> pure $ LenientVersion.version result
387-
388-
Legacy.Manifest.fetchLegacyManifest payload.name address (RawVersion payload.ref) >>= case _ of
389-
Left manifestError -> do
390-
let formatError { error, reason } = reason <> " " <> Legacy.Manifest.printLegacyManifestError error
391-
Except.throw $ String.joinWith "\n"
392-
[ "Could not publish your package because there were issues converting its spago.dhall and/or bower.json files into a purs.json manifest:"
393-
, formatError manifestError
394-
]
395-
Right legacyManifest -> do
396-
Log.debug $ "Successfully produced a legacy manifest from the package source. Writing it to the package source..."
397-
let manifest = Legacy.Manifest.toManifest payload.name version existingMetadata.location legacyManifest
398-
Run.liftAff $ writeJsonFile Manifest.codec packagePursJson manifest
383+
let packageSpagoYaml = Path.concat [ packageDirectory, "spago.yaml" ]
384+
hasSpagoYaml <- Run.liftEffect $ FS.Sync.exists packageSpagoYaml
385+
case hasSpagoYaml of
386+
true -> do
387+
Notify.notify $ "Package source does not have a purs.json file, creating one from your spago.yaml file..."
388+
-- Need to make a Spago log env first, disable the logging
389+
let spagoEnv = { logOptions: { color: false, verbosity: Spago.Log.LogQuiet } }
390+
maybeConfig <- Spago.Prelude.runSpago spagoEnv (Spago.readConfig packageSpagoYaml)
391+
case maybeConfig of
392+
Left err' -> Except.throw $ String.joinWith "\n"
393+
[ "Could not publish your package - a spago.yaml was present, but it was not possible to read it:"
394+
, err'
395+
]
396+
Right { yaml: config } -> do
397+
-- Once we have the config we are still not entirely sure it fits into a Manifest
398+
-- E.g. need to make sure all the ranges are present
399+
case spagoToManifest config of
400+
Left err -> Except.throw $ String.joinWith "\n"
401+
[ "Could not publish your package - there was an error while converting your spago.yaml into a purs.json manifest:"
402+
, err
403+
]
404+
Right manifest -> do
405+
Log.debug "Successfully converted a spago.yaml into a purs.json manifest"
406+
Run.liftAff $ writeJsonFile Manifest.codec packagePursJson manifest
407+
false -> do
408+
Notify.notify $ "Package source does not have a purs.json file. Creating one from your bower.json and/or spago.dhall files..."
409+
address <- case existingMetadata.location of
410+
Git _ -> Except.throw "Legacy packages can only come from GitHub."
411+
GitHub { subdir: Just subdir } -> Except.throw $ "Legacy packages cannot use the 'subdir' key, but this package specifies a " <> subdir <> " subdir."
412+
GitHub { owner, repo } -> pure { owner, repo }
413+
414+
version <- case LenientVersion.parse payload.ref of
415+
Left _ -> Except.throw $ "The provided ref " <> payload.ref <> " is not a version of the form X.Y.Z or vX.Y.Z, so it cannot be used."
416+
Right result -> pure $ LenientVersion.version result
417+
418+
Legacy.Manifest.fetchLegacyManifest payload.name address (RawVersion payload.ref) >>= case _ of
419+
Left manifestError -> do
420+
let formatError { error, reason } = reason <> " " <> Legacy.Manifest.printLegacyManifestError error
421+
Except.throw $ String.joinWith "\n"
422+
[ "Could not publish your package because there were issues converting its spago.dhall and/or bower.json files into a purs.json manifest:"
423+
, formatError manifestError
424+
]
425+
Right legacyManifest -> do
426+
Log.debug $ "Successfully produced a legacy manifest from the package source. Writing it to the package source..."
427+
let manifest = Legacy.Manifest.toManifest payload.name version existingMetadata.location legacyManifest
428+
Run.liftAff $ writeJsonFile Manifest.codec packagePursJson manifest
399429

400430
Log.debug "A valid purs.json is available in the package source."
401431

@@ -981,3 +1011,28 @@ getPacchettiBotti = do
9811011

9821012
packagingTeam :: Team
9831013
packagingTeam = { org: "purescript", team: "packaging" }
1014+
1015+
spagoToManifest :: Spago.Config -> Either String Manifest
1016+
spagoToManifest config = do
1017+
package@{ name, description, dependencies: Spago.Dependencies deps } <- note "Did not find a package in the config" config.package
1018+
publishConfig@{ version, license } <- note "Did not find a `publish` section in the package config" package.publish
1019+
location <- note "Did not find a `location` field in the publish config" publishConfig.location
1020+
let
1021+
checkRange :: Tuple PackageName (Maybe Range) -> Either PackageName (Tuple PackageName Range)
1022+
checkRange (Tuple packageName maybeRange) = case maybeRange of
1023+
Nothing -> Left packageName
1024+
Just r -> Right (Tuple packageName r)
1025+
let { fail: failedPackages, success } = partitionEithers $ map checkRange (Map.toUnfoldable deps :: Array _)
1026+
dependencies <- case failedPackages of
1027+
[] -> Right (Map.fromFoldable success)
1028+
errs -> Left $ "The following packages did not have their ranges specified: " <> String.joinWith ", " (map PackageName.print errs)
1029+
pure $ Manifest
1030+
{ version
1031+
, license
1032+
, name
1033+
, location
1034+
, description
1035+
, dependencies
1036+
, owners: Nothing -- TODO Spago still needs to add this to its config
1037+
, files: Nothing -- TODO Spago still needs to add this to its config
1038+
}

flake.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@
7878
scripts = pkgs.symlinkJoin {
7979
name = "scripts";
8080
paths = pkgs.lib.mapAttrsToList pkgs.writeShellScriptBin {
81-
registry-install = ''
81+
registry-build = ''
8282
cd $(git rev-parse --show-toplevel)
8383
npm ci
84-
spago install
84+
spago build
8585
'';
8686

8787
registry-test = ''

package-lock.json

+18-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"workspaces": [
66
"app",
7+
"foreign",
78
"lib"
89
]
910
}

spago.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
workspace:
22
package_set:
3-
registry: 2.2.0
3+
registry: 11.10.0
4+
extra_packages:
5+
spago-core:
6+
git: https://github.com/purescript/spago.git
7+
ref: ad81065f577436185bd0fe4bfb88714363ffca15
8+
subdir: spaghetto/core

0 commit comments

Comments
 (0)