Skip to content

Commit

Permalink
[garn init] Handle non-inferrable projects better (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
soenkehahn authored Nov 2, 2023
1 parent 007e104 commit 3c4852f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
25 changes: 24 additions & 1 deletion test/spec/InitSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module InitSpec where

import Control.Monad (forM_, when)
import Data.String.Interpolate (i)
import Data.String.Interpolate.Util (unindent)
import Development.Shake (StdoutTrim (..), cmd, cmd_)
Expand Down Expand Up @@ -129,10 +130,32 @@ spec = do
stderr output
`shouldBe` unindent
[i|
[garn] Creating a garn.ts file
[garn] Found but could not parse cabal file
[garn] Cannot detect any project toolchains, sorry! Creating example garn.ts file
|]

describe "when no specific initializer runs" $ do
it "prints out a message about that" $ \onTestFailureLog -> do
output <- runGarn ["init"] "" repoDir Nothing
onTestFailureLog output
stderr output
`shouldBe` unindent
[i|
[garn] Cannot detect any project toolchains, sorry! Creating example garn.ts file
|]

it "generates a commented file, except for imports" $ \onTestFailureLog -> do
output <- runGarn ["init"] "" repoDir Nothing
onTestFailureLog output
garnFile <- lines <$> readFile "garn.ts"
take 2 garnFile
`shouldBe` [ "import * as garn from \"https://garn.io/ts/v0.0.14/mod.ts\";",
"import * as pkgs from \"https://garn.io/ts/v0.0.14/nixpkgs.ts\";"
]
forM_ (drop 2 garnFile) $ \line ->
when (line /= "") $
line `shouldStartWith` "//"

rewriteImportsToLocalhost :: IO ()
rewriteImportsToLocalhost = do
cmd_ "sd" "https://garn.io/ts/v[0-9]+\\.[0-9]+\\.[0-9]+/" "http://localhost:8777/" "garn.ts"
66 changes: 40 additions & 26 deletions ts/internal/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const initializers = [
...javascript.initializers,
];

console.error("[garn] Creating a garn.ts file");

for (const init of initializers) {
const result = init(Deno.cwd());
switch (result.tag) {
Expand All @@ -34,45 +32,61 @@ for (const init of initializers) {
}

if (initializedSections.length === 0) {
console.error(
"[garn] Cannot detect any project toolchains, sorry! Creating example garn.ts file",
);
initializedSections.push(outdent`
// Welcome to garn! \`garn init\` was unable to find any existing supported
// projects, but it is easy to get started!
// Check out the language helper functions under garn.go, garn.haskell, and
// garn.javascript.
//
// For example:
export const myGoProject = garn.go.mkGoProject({
description: "My go project",
src: "./my-go-project",
goVersion: "1.20",
});
export const myHaskellProject = garn.haskell.mkHaskellProject({
description: "My haskell project",
executable: "server",
compiler: "ghc94",
src: "./my-haskell-project",
});
export const myNodeProject = garn.javascript.mkNpmProject({
description: "My node project",
src: "./my-node-project",
nodeVersion: "18",
});
// export const myGoProject = garn.go.mkGoProject({
// description: "My go project",
// src: "./my-go-project",
// goVersion: "1.20",
// });
// export const myHaskellProject = garn.haskell.mkHaskellProject({
// description: "My haskell project",
// executable: "server",
// compiler: "ghc94",
// src: "./my-haskell-project",
// });
// export const myNodeProject = garn.javascript.mkNpmProject({
// description: "My node project",
// src: "./my-node-project",
// nodeVersion: "18",
// });
// You can also manually create environments and projects. For example
// uncomment this block and you can run \`garn enter myProject\` to be put into a
// shell with cowsay installed, and \`garn run myProject\` to execute the default
// executable for this project.
const myProjectEnvironment = garn.mkEnvironment().withDevTools([pkgs.cowsay]);
export const myProject = garn.mkProject({
description: "My project",
defaultEnvironment: myProjectEnvironment,
defaultExecutable: myProjectEnvironment.shell\`cowsay "Hello from garn!"\`,
}, {});
// const myProjectEnvironment = garn.mkEnvironment().withDevTools([pkgs.cowsay]);
// export const myProject = garn.mkProject({
// description: "My project",
// defaultEnvironment: myProjectEnvironment,
// defaultExecutable: myProjectEnvironment.shell\`cowsay "Hello from garn!"\`,
// }, {});
// In order to get an isolated vscodium that ships with Deno and is
// pre-configured to use Deno's LSP, you can use the following:
// Run with \`garn run edit\`.
// export const edit = garn.editGarnConfig;
`);
} else {
console.error("[garn] Creating a garn.ts file");
}

Deno.writeTextFileSync(
Expand Down

0 comments on commit 3c4852f

Please sign in to comment.