Skip to content

Commit

Permalink
add node_version and yarn_version options
Browse files Browse the repository at this point in the history
  • Loading branch information
onshi committed Aug 6, 2024
1 parent 1a55b7f commit 9ebd6dc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 25 deletions.
102 changes: 82 additions & 20 deletions src/docker_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ impl DockerClient {
pub fn build_image(
ruby_version: &str,
rails_version: &str,
node_version: &str,
yarn_version: &str,
user_id: Option<u32>,
group_id: Option<u32>,
) -> Command {
Expand All @@ -15,6 +17,8 @@ impl DockerClient {

Self::set_build_arg(&mut command, "RUBY_VERSION", ruby_version);
Self::set_build_arg(&mut command, "RAILS_VERSION", rails_version);
Self::set_build_arg(&mut command, "NODE_VERSION", node_version);
Self::set_build_arg(&mut command, "YARN_VERSION", yarn_version);

if let Some(id) = user_id {
Self::set_build_arg(&mut command, "USER_ID", &id.to_string())
Expand All @@ -25,27 +29,56 @@ impl DockerClient {

command.arg("-t");

Self::set_image_name(&mut command, ruby_version, rails_version);
Self::set_image_name(
&mut command,
ruby_version,
rails_version,
node_version,
yarn_version,
);

command.arg("-").stdin(Stdio::piped());

command
}

pub fn run_image(ruby_version: &str, rails_version: &str, args: Vec<String>) -> Command {
pub fn run_image(
ruby_version: &str,
rails_version: &str,
node_version: &str,
yarn_version: &str,
args: Vec<String>,
) -> Command {
let mut command = Self::run();

Self::set_workdir(&mut command);
Self::set_image_name(&mut command, ruby_version, rails_version);
Self::set_image_name(
&mut command,
ruby_version,
rails_version,
node_version,
yarn_version,
);
Self::set_rails_new(&mut command, args);

command
}

pub fn get_help(ruby_version: &str, rails_version: &str) -> Command {
pub fn get_help(
ruby_version: &str,
rails_version: &str,
node_version: &str,
yarn_version: &str,
) -> Command {
let mut command = Self::run();

Self::set_image_name(&mut command, ruby_version, rails_version);
Self::set_image_name(
&mut command,
ruby_version,
rails_version,
node_version,
yarn_version,
);
Self::set_rails_new(&mut command, vec!["--help".to_string()]);

command
Expand Down Expand Up @@ -73,8 +106,17 @@ impl DockerClient {
.args(["-w", current_dir]);
}

fn set_image_name(command: &mut Command, ruby_version: &str, rails_version: &str) {
command.arg(format!("rails-new-{}-{}", ruby_version, rails_version));
fn set_image_name(
command: &mut Command,
ruby_version: &str,
rails_version: &str,
node_version: &str,
yarn_version: &str,
) {
command.arg(format!(
"rails-new-ruby-{}-rails-{}-node-{}-yarn-{}",
ruby_version, rails_version, node_version, yarn_version,
));
}

fn set_rails_new(command: &mut Command, args: Vec<String>) {
Expand All @@ -89,7 +131,7 @@ mod tests {

#[test]
fn build_image() {
let command = DockerClient::build_image("3.2.3", "7.1.3", None, None);
let command = DockerClient::build_image("3.2.4", "7.1.3", "22.1", "1.22.22", None, None);

assert_eq!(command.get_program(), "docker");

Expand All @@ -100,19 +142,24 @@ mod tests {
&[
"build",
"--build-arg",
"RUBY_VERSION=3.2.3",
"RUBY_VERSION=3.2.4",
"--build-arg",
"RAILS_VERSION=7.1.3",
"--build-arg",
"NODE_VERSION=22.1",
"--build-arg",
"YARN_VERSION=1.22.22",
"-t",
"rails-new-3.2.3-7.1.3",
"rails-new-ruby-3.2.4-rails-7.1.3-node-22.1-yarn-1.22.22",
"-",
]
);
}

#[test]
fn build_image_with_user_id() {
let command = DockerClient::build_image("3.2.3", "7.1.3", Some(1000), None);
let command =
DockerClient::build_image("3.2.4", "7.1.3", "22.1", "1.22.22", Some(1000), None);

assert_eq!(command.get_program(), "docker");

Expand All @@ -123,21 +170,26 @@ mod tests {
&[
"build",
"--build-arg",
"RUBY_VERSION=3.2.3",
"RUBY_VERSION=3.2.4",
"--build-arg",
"RAILS_VERSION=7.1.3",
"--build-arg",
"NODE_VERSION=22.1",
"--build-arg",
"YARN_VERSION=1.22.22",
"--build-arg",
"USER_ID=1000",
"-t",
"rails-new-3.2.3-7.1.3",
"rails-new-ruby-3.2.4-rails-7.1.3-node-22.1-yarn-1.22.22",
"-",
]
);
}

#[test]
fn build_image_with_group_id() {
let command = DockerClient::build_image("3.2.3", "7.1.3", None, Some(1000));
let command =
DockerClient::build_image("3.2.4", "7.1.3", "22.1", "1.22.22", None, Some(1000));

assert_eq!(command.get_program(), "docker");

Expand All @@ -148,21 +200,31 @@ mod tests {
&[
"build",
"--build-arg",
"RUBY_VERSION=3.2.3",
"RUBY_VERSION=3.2.4",
"--build-arg",
"RAILS_VERSION=7.1.3",
"--build-arg",
"NODE_VERSION=22.1",
"--build-arg",
"YARN_VERSION=1.22.22",
"--build-arg",
"GROUP_ID=1000",
"-t",
"rails-new-3.2.3-7.1.3",
"rails-new-ruby-3.2.4-rails-7.1.3-node-22.1-yarn-1.22.22",
"-",
]
);
}

#[test]
fn run_image() {
let command = DockerClient::run_image("3.2.3", "7.1.3", vec!["my_app".to_string()]);
let command = DockerClient::run_image(
"3.2.4",
"7.1.3",
"22.1",
"1.22.22",
vec!["my_app".to_string()],
);

assert_eq!(command.get_program(), "docker");

Expand All @@ -180,7 +242,7 @@ mod tests {
&format!("{}:{}", current_dir, current_dir),
"-w",
current_dir,
"rails-new-3.2.3-7.1.3",
"rails-new-ruby-3.2.4-rails-7.1.3-node-22.1-yarn-1.22.22",
"rails",
"new",
"my_app",
Expand All @@ -190,7 +252,7 @@ mod tests {

#[test]
fn get_help() {
let command = DockerClient::get_help("3.2.3", "7.1.3");
let command = DockerClient::get_help("3.2.4", "7.1.3", "22.1", "1.22.22");

assert_eq!(command.get_program(), "docker");

Expand All @@ -201,7 +263,7 @@ mod tests {
&[
"run",
"--rm",
"rails-new-3.2.3-7.1.3",
"rails-new-ruby-3.2.4-rails-7.1.3-node-22.1-yarn-1.22.22",
"rails",
"new",
"--help",
Expand Down
23 changes: 20 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ fn main() {

let ruby_version = cli.ruby_version;
let rails_version = cli.rails_version;
let node_version = cli.node_version;
let yarn_version = cli.yarn_version;

// Run docker build --build-arg RUBY_VERSION=$RUBY_VERSION --build-arg RAILS_VERSION=$RAILS_VERSION -t rails-new-$RUBY_VERSION-$RAILS_VERSION
// Run
// docker build \
// --build-arg RUBY_VERSION=$RUBY_VERSION \
// --build-arg RAILS_VERSION=$RAILS_VERSION \
// --build-arg NODE_VERSION=$NODE_VERSION \
// --build-arg YARN_VERSION=$YARN_VERSION \
// -t rails-new-ruby-$RUBY_VERSION-rails-$RAILS_VERSION-node-$NODE_VERSION-yarn-$YARN_VERSION
// passing the content of DOCKERFILE to the command stdin
let mut child = DockerClient::build_image(
&ruby_version,
&rails_version,
&node_version,
&yarn_version,
os_specific::get_user_id(),
os_specific::get_group_id(),
)
Expand All @@ -44,12 +54,19 @@ fn main() {

match &cli.command {
Some(Commands::RailsHelp {}) => {
command = DockerClient::get_help(&ruby_version, &rails_version)
command =
DockerClient::get_help(&ruby_version, &rails_version, &node_version, &yarn_version)
}

None => {
// Run the image with docker run -v $(pwd):/$(pwd) -w $(pwd) rails-new-$RUBY_VERSION-$RAILS_VERSION rails new $@
command = DockerClient::run_image(&ruby_version, &rails_version, cli.args)
command = DockerClient::run_image(
&ruby_version,
&rails_version,
&node_version,
&yarn_version,
cli.args,
)
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/rails_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ pub struct Cli {
#[clap(trailing_var_arg = true, required = true)]
/// arguments passed to `rails new`
pub args: Vec<String>,
#[clap(long, short = 'u', default_value = "3.3.3")]
#[clap(long, short = 'u', default_value = "3.3.4")]
pub ruby_version: String,
#[clap(long, short = 'r', default_value = "7.1.3")]
pub rails_version: String,
#[clap(long, short = 'n', default_value = "22.1")]
pub node_version: String,
#[clap(long, short = 'y', default_value = "1.22.22")]
pub yarn_version: String,

#[command(subcommand)]
pub command: Option<Commands>,
Expand Down Expand Up @@ -53,9 +57,13 @@ mod tests {

let ruby_version = m.get_one::<String>("ruby_version").unwrap();
let rails_version = m.get_one::<String>("rails_version").unwrap();
let node_version = m.get_one::<String>("node_version").unwrap();
let yarn_version = m.get_one::<String>("yarn_version").unwrap();

assert_eq!(ruby_version, "3.3.3");
assert_eq!(ruby_version, "3.3.4");
assert_eq!(rails_version, "7.1.3");
assert_eq!(node_version, "22.1");
assert_eq!(yarn_version, "1.22.22");

Ok(())
}
Expand Down

0 comments on commit 9ebd6dc

Please sign in to comment.