Skip to content

Commit

Permalink
feat(github-actions): add manual project type (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickard Natt och Dag authored Dec 11, 2020
1 parent 39dca91 commit fcf8805
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 43 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ categories = ["command-line-utilities"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "2.33.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
structopt = "0.3.13"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ and tweaks the config files:

#### Flags

- `--no-npm` - Turn off `@semantic-release/npm` in `.releaserc` and remove `NPM_TOKEN` secret from `release.yml`
- `--no-npm`, `-n` - Turn off `@semantic-release/npm` in `.releaserc` and remove `NPM_TOKEN` secret from `release.yml`
- `--project`, `-p` - Pass a supported project type [javascript, rescript, rust]

#### Environment variables

Expand Down
15 changes: 11 additions & 4 deletions src/commands/github_actions.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
use crate::utils::{helpers, project::Project, template};
use crate::utils::{
helpers,
project::{Project, ProjectType},
template,
};
use helpers::Result;
use serde_json::json;

pub fn run(no_npm: bool) -> Result<()> {
let project = Project::new();
pub fn run(no_npm: bool, project_type: Option<ProjectType>) -> Result<()> {
let project = match project_type {
Some(project_type) => Project::from_project_type(project_type),
None => Project::new(),
};

project.log();

let data = json!({ "name": env!("CARGO_PKG_NAME"), "noNpm": no_npm });
let data = json!({ "name": env!("CARGO_PKG_NAME"), "noNpm": !no_npm });

template::render_dir(project.directory(), ".github/workflows", &data)?;
template::render_file(&project.release_config(), ".releaserc", Some(&data))?;
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod utils;

use commands::{add, github_actions, graphql, rescript};
use structopt::StructOpt;
use utils::helpers::Result;
use utils::{helpers::Result, project::ProjectType};

#[derive(Debug, StructOpt)]
enum AddCommand {
Expand Down Expand Up @@ -32,8 +32,12 @@ enum Cli {
/// Add GitHub actions
GithubActions {
/// Remove release to npm
#[structopt(long = "no-npm", short = "n")]
#[structopt(long, short, parse(from_flag= std::ops::Not::not))]
no_npm: bool,

/// Project type
#[structopt(long, short, possible_values = &ProjectType::variants(), case_insensitive = true)]
project: Option<ProjectType>,
},

/// Create GraphQL API
Expand All @@ -54,7 +58,7 @@ pub fn run() -> Result<()> {
Cli::Add(AddCommand::Jest) => add::jest()?,
Cli::Add(AddCommand::Nvm) => add::nvm()?,
Cli::Add(AddCommand::Prettier) => add::prettier()?,
Cli::GithubActions { no_npm } => github_actions::run(no_npm)?,
Cli::GithubActions { no_npm, project } => github_actions::run(no_npm, project)?,
Cli::Graphql { name } => graphql::run(name)?,
Cli::Rescript { name } => rescript::run(name)?,
};
Expand Down
72 changes: 37 additions & 35 deletions src/utils/project.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
use crate::utils::format;
use clap::arg_enum;
use dialoguer::{theme::ColorfulTheme, Select};
use include_dir_macro::include_dir;
use std::fs;
use std::{collections, path};

pub enum T {
JavaScript,
ReScript,
Rust,
}

impl std::fmt::Display for T {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
T::JavaScript => write!(f, "JavaScript"),
T::ReScript => write!(f, "ReScript"),
T::Rust => write!(f, "Rust"),
}
arg_enum! {
#[derive(Debug)]
pub enum ProjectType {
JavaScript,
ReScript,
Rust,
}
}

fn from_selection() -> T {
fn from_selection() -> ProjectType {
let selections = &["ReScript", "Rust"];
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Multiple languages found, select one:")
Expand All @@ -30,55 +24,63 @@ fn from_selection() -> T {
.unwrap();

match selection {
0 => T::ReScript,
1 => T::Rust,
0 => ProjectType::ReScript,
1 => ProjectType::Rust,
_ => panic!("Unknown selection"),
}
}

pub fn make() -> T {
pub fn make() -> ProjectType {
let has_bs_config = fs::metadata("bsconfig.json").is_ok();
let has_cargo_toml = fs::metadata("Cargo.toml").is_ok();

match (has_bs_config, has_cargo_toml) {
(true, false) => T::ReScript,
(false, true) => T::Rust,
(true, false) => ProjectType::ReScript,
(false, true) => ProjectType::Rust,
(true, true) => from_selection(),
_ => T::JavaScript,
_ => ProjectType::JavaScript,
}
}

pub struct Project {
app_type: T,
project_type: ProjectType,
}

impl Project {
pub fn directory(&self) -> collections::HashMap<&'static path::Path, &'static [u8]> {
match self.app_type {
T::ReScript => include_dir!("src/templates/github_actions/rescript"),
T::JavaScript => include_dir!("src/templates/github_actions/js"),
T::Rust => include_dir!("src/templates/github_actions/rust"),
match self.project_type {
ProjectType::ReScript => include_dir!("src/templates/github_actions/rescript"),
ProjectType::JavaScript => include_dir!("src/templates/github_actions/js"),
ProjectType::Rust => include_dir!("src/templates/github_actions/rust"),
}
}

pub fn release_config(&self) -> &'static str {
match self.app_type {
T::ReScript => include_str!("../templates/github_actions/release_config/.releaserc.js"),
T::JavaScript => {
match self.project_type {
ProjectType::ReScript => {
include_str!("../templates/github_actions/release_config/.releaserc.js")
}
ProjectType::JavaScript => {
include_str!("../templates/github_actions/release_config/.releaserc.js")
}
T::Rust => include_str!("../templates/github_actions/release_config/.releaserc.rs"),
ProjectType::Rust => {
include_str!("../templates/github_actions/release_config/.releaserc.rs")
}
}
}

pub fn log(&self) {
format::success(&format!("Found {} project", self.app_type))
format::success(&format!("Found {} project", self.project_type))
}

pub fn from_project_type(project_type: ProjectType) -> Project {
Project { project_type }
}

pub fn new() -> Project {
let app_type = make();
let project_type = make();

Project { app_type }
Project { project_type }
}
}

Expand All @@ -88,16 +90,16 @@ mod tests {

#[test]
fn js_project_string() {
assert_eq!(format!("{}", &T::JavaScript), "JavaScript")
assert_eq!(format!("{}", &ProjectType::JavaScript), "JavaScript")
}

#[test]
fn rust_project_string() {
assert_eq!(format!("{}", &T::Rust), "Rust")
assert_eq!(format!("{}", &ProjectType::Rust), "Rust")
}

#[test]
fn rescript_project_string() {
assert_eq!(format!("{}", &T::ReScript), "ReScript")
assert_eq!(format!("{}", &ProjectType::ReScript), "ReScript")
}
}

0 comments on commit fcf8805

Please sign in to comment.