Skip to content

Commit

Permalink
CLI: Add javascript as option for template flag to new (#3395)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tar-Tarus authored Sep 30, 2024
1 parent 5efe8d3 commit fcfc553
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

### Build tool

- The `--template` flag for `gleam new` takes the values `erlang` and
`javascript` to specify what target to use, with `erlang` being the default.
([Mohammed Khouni](https://github.com/Tar-Tarus))

### Compiler

- The compiler now prints correctly qualified or aliased type names when
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ thiserror = "1"
# Test assertion errors with diffs
pretty_assertions = "1"
# Snapshot testing to make test maintenance easier
insta = "1"
insta = {version = "1", features = ["glob"]}
# A transitive dependency needed to compile into wasm32-unknown-unknown
# See https://docs.rs/getrandom/latest/getrandom/index.html#webassembly-support
getrandom = { version = "0", features = ["js"] }
1 change: 1 addition & 0 deletions compiler-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ walkdir.workspace = true
# Creation of temporary directories
tempfile = "3"
pretty_assertions.workspace = true
insta.workspace = true

[build-dependencies]
# For statically linking the VCRuntime on Windows when
Expand Down
6 changes: 5 additions & 1 deletion compiler-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ enum Command {
Export(ExportTarget),
}

fn template_doc() -> &'static str {
"The template to use"
}

fn target_doc() -> String {
format!("The platform to target ({})", Target::VARIANTS.join("|"))
}
Expand Down Expand Up @@ -288,7 +292,7 @@ pub struct NewOptions {
#[arg(long)]
pub name: Option<String>,

#[arg(long, ignore_case = true, default_value = "lib")]
#[arg(long, ignore_case = true, default_value = "erlang", help = template_doc())]
pub template: new::Template,

/// Skip git initialization and creation of .gitignore, .git/* and .github/* files
Expand Down
14 changes: 11 additions & 3 deletions compiler-cli/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ const ELIXIR_VERSION: &str = "1.15.4";
#[derive(
Debug, Serialize, Deserialize, Display, EnumString, VariantNames, ValueEnum, Clone, Copy,
)]
#[strum(serialize_all = "kebab_case")]
#[strum(serialize_all = "lowercase")]
#[clap(rename_all = "lower")]
pub enum Template {
#[clap(skip)]
Lib,
Erlang,
JavaScript,
}

#[derive(Debug)]
Expand Down Expand Up @@ -74,6 +78,10 @@ impl FileToCreate {
let skip_git = creator.options.skip_git;
let skip_github = creator.options.skip_github;
let gleam_version = creator.gleam_version;
let target = match creator.options.template {
Template::JavaScript => "target = \"javascript\"\n",
Template::Lib | Template::Erlang => "",
};

match self {
Self::Readme => Some(format!(
Expand Down Expand Up @@ -142,7 +150,7 @@ pub fn hello_world_test() {
Self::GleamToml => Some(format!(
r#"name = "{project_name}"
version = "1.0.0"
{target}
# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
Expand Down Expand Up @@ -241,7 +249,7 @@ impl Creator {
}

match self.options.template {
Template::Lib => {
Template::Lib | Template::Erlang | Template::JavaScript => {
for file in FileToCreate::iter() {
let path = file.location(self);
if let Some(contents) = file.contents(self) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: compiler-cli/src/new/tests.rs
expression: "crate::fs::read(path.join(\"gleam.toml\")).unwrap()"
---
name = "my_project"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
8 changes: 8 additions & 0 deletions compiler-cli/src/new/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: compiler-cli/src/new/tests.rs
expression: "crate::fs::read(Utf8PathBuf::from_path_buf(file_path.to_path_buf()).expect(\"Non Utf8 Path\")).unwrap()"
---
*.beam
*.ez
/build
erl_crash.dump
28 changes: 28 additions & 0 deletions compiler-cli/src/new/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: compiler-cli/src/new/tests.rs
expression: "crate::fs::read(Utf8PathBuf::from_path_buf(file_path.to_path_buf()).expect(\"Non Utf8 Path\")).unwrap()"
---
# my_project

[![Package Version](https://img.shields.io/hexpm/v/my_project)](https://hex.pm/packages/my_project)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/my_project/)

```sh
gleam add my_project@1
```
```gleam
import my_project
pub fn main() {
// TODO: An example of the project in use
}
```

Further documentation can be found at <https://hexdocs.pm/my_project>.

## Development

```sh
gleam run # Run the project
gleam test # Run the tests
```
23 changes: 23 additions & 0 deletions compiler-cli/src/new/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: compiler-cli/src/new/tests.rs
expression: "crate::fs::read(Utf8PathBuf::from_path_buf(file_path.to_path_buf()).expect(\"Non Utf8 Path\")).unwrap()"
---
name = "my_project"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: compiler-cli/src/new/tests.rs
expression: "crate::fs::read(path.join(\"gleam.toml\")).unwrap()"
---
name = "my_project"
version = "1.0.0"
target = "javascript"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
8 changes: 8 additions & 0 deletions ...ler-cli/src/new/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: compiler-cli/src/new/tests.rs
expression: "crate::fs::read(Utf8PathBuf::from_path_buf(file_path.to_path_buf()).expect(\"Non Utf8 Path\")).unwrap()"
---
*.beam
*.ez
/build
erl_crash.dump
28 changes: 28 additions & 0 deletions ...iler-cli/src/new/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: compiler-cli/src/new/tests.rs
expression: "crate::fs::read(Utf8PathBuf::from_path_buf(file_path.to_path_buf()).expect(\"Non Utf8 Path\")).unwrap()"
---
# my_project

[![Package Version](https://img.shields.io/hexpm/v/my_project)](https://hex.pm/packages/my_project)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/my_project/)

```sh
gleam add my_project@1
```
```gleam
import my_project
pub fn main() {
// TODO: An example of the project in use
}
```

Further documentation can be found at <https://hexdocs.pm/my_project>.

## Development

```sh
gleam run # Run the project
gleam test # Run the tests
```
24 changes: 24 additions & 0 deletions ...ler-cli/src/new/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: compiler-cli/src/new/tests.rs
expression: "crate::fs::read(Utf8PathBuf::from_path_buf(file_path.to_path_buf()).expect(\"Non Utf8 Path\")).unwrap()"
---
name = "my_project"
version = "1.0.0"
target = "javascript"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
Loading

0 comments on commit fcfc553

Please sign in to comment.