Skip to content

Commit 89e44af

Browse files
committed
Remove default Rails version
This enables `gem install rails` in the Dockerfile to always install the latest Rails version instead of the default value in `rails-new`.
1 parent 68a2913 commit 89e44af

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

src/docker_client.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub struct DockerClient {}
55
impl DockerClient {
66
pub fn build_image(
77
ruby_version: &str,
8-
rails_version: &str,
8+
maybe_rails_version: Option<&str>,
99
user_id: Option<u32>,
1010
group_id: Option<u32>,
1111
rebuild: bool,
@@ -19,7 +19,9 @@ impl DockerClient {
1919
}
2020

2121
Self::set_build_arg(&mut command, "RUBY_VERSION", ruby_version);
22-
Self::set_build_arg(&mut command, "RAILS_VERSION", rails_version);
22+
if let Some(rails_version) = maybe_rails_version {
23+
Self::set_build_arg(&mut command, "RAILS_VERSION", rails_version);
24+
}
2325

2426
if let Some(id) = user_id {
2527
Self::set_build_arg(&mut command, "USER_ID", &id.to_string())
@@ -30,14 +32,18 @@ impl DockerClient {
3032

3133
command.arg("-t");
3234

33-
Self::set_image_name(&mut command, ruby_version, rails_version);
35+
Self::set_image_name(&mut command, ruby_version, maybe_rails_version);
3436

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

3739
command
3840
}
3941

40-
pub fn run_image(ruby_version: &str, rails_version: &str, args: Vec<String>) -> Command {
42+
pub fn run_image(
43+
ruby_version: &str,
44+
rails_version: Option<&str>,
45+
args: Vec<String>,
46+
) -> Command {
4147
let mut command = Self::run();
4248

4349
Self::set_workdir(&mut command);
@@ -47,7 +53,7 @@ impl DockerClient {
4753
command
4854
}
4955

50-
pub fn get_help(ruby_version: &str, rails_version: &str) -> Command {
56+
pub fn get_help(ruby_version: &str, rails_version: Option<&str>) -> Command {
5157
let mut command = Self::run();
5258

5359
Self::set_image_name(&mut command, ruby_version, rails_version);
@@ -81,8 +87,16 @@ impl DockerClient {
8187
.args(["-w", current_dir]);
8288
}
8389

84-
fn set_image_name(command: &mut Command, ruby_version: &str, rails_version: &str) {
85-
command.arg(format!("rails-new-{}-{}", ruby_version, rails_version));
90+
fn set_image_name(
91+
command: &mut Command,
92+
ruby_version: &str,
93+
maybe_rails_version: Option<&str>,
94+
) {
95+
if let Some(rails_version) = maybe_rails_version {
96+
command.arg(format!("rails-new-{}-{}", ruby_version, rails_version));
97+
} else {
98+
command.arg(format!("rails-new-{}", ruby_version));
99+
}
86100
}
87101

88102
fn set_rails_new(command: &mut Command, args: Vec<String>) {
@@ -121,7 +135,7 @@ mod tests {
121135

122136
#[test]
123137
fn build_image() {
124-
let command = DockerClient::build_image("3.2.3", "7.1.3", None, None, false);
138+
let command = DockerClient::build_image("3.2.3", Some("7.1.3"), None, None, false);
125139

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

@@ -144,7 +158,7 @@ mod tests {
144158

145159
#[test]
146160
fn build_image_with_user_id() {
147-
let command = DockerClient::build_image("3.2.3", "7.1.3", Some(1000), None, false);
161+
let command = DockerClient::build_image("3.2.3", Some("7.1.3"), Some(1000), None, false);
148162

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

@@ -169,7 +183,7 @@ mod tests {
169183

170184
#[test]
171185
fn build_image_with_group_id() {
172-
let command = DockerClient::build_image("3.2.3", "7.1.3", None, Some(1000), false);
186+
let command = DockerClient::build_image("3.2.3", Some("7.1.3"), None, Some(1000), false);
173187

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

@@ -194,7 +208,7 @@ mod tests {
194208

195209
#[test]
196210
fn run_image() {
197-
let command = DockerClient::run_image("3.2.3", "7.1.3", vec!["my_app".to_string()]);
211+
let command = DockerClient::run_image("3.2.3", Some("7.1.3"), vec!["my_app".to_string()]);
198212

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

@@ -223,7 +237,7 @@ mod tests {
223237

224238
#[test]
225239
fn get_help() {
226-
let command = DockerClient::get_help("3.2.3", "7.1.3");
240+
let command = DockerClient::get_help("3.2.3", Some("7.1.3"));
227241

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

src/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ fn main() {
1818
let cli = Cli::parse();
1919

2020
let ruby_version = cli.ruby_version;
21-
let rails_version = cli.rails_version;
21+
let rails_version = cli.rails_version.as_deref();
2222
let rebuild = cli.rebuild;
2323

2424
// Run docker build --build-arg RUBY_VERSION=$RUBY_VERSION --build-arg RAILS_VERSION=$RAILS_VERSION -t rails-new-$RUBY_VERSION-$RAILS_VERSION
2525
// passing the content of DOCKERFILE to the command stdin
2626
let mut child = DockerClient::build_image(
2727
&ruby_version,
28-
&rails_version,
28+
rails_version,
2929
os_specific::get_user_id(),
3030
os_specific::get_group_id(),
3131
rebuild,
@@ -46,12 +46,12 @@ fn main() {
4646

4747
match &cli.command {
4848
Some(Commands::RailsHelp {}) => {
49-
command = DockerClient::get_help(&ruby_version, &rails_version)
49+
command = DockerClient::get_help(&ruby_version, rails_version)
5050
}
5151

5252
None => {
5353
// Run the image with docker run -v $(pwd):/$(pwd) -w $(pwd) rails-new-$RUBY_VERSION-$RAILS_VERSION rails new $@
54-
command = DockerClient::run_image(&ruby_version, &rails_version, cli.args)
54+
command = DockerClient::run_image(&ruby_version, rails_version, cli.args)
5555
}
5656
}
5757

src/rails_new.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub struct Cli {
88
pub args: Vec<String>,
99
#[clap(long, short = 'u', default_value = "3.4.1")]
1010
pub ruby_version: String,
11-
#[clap(long, short = 'r', default_value = "8.0.1")]
12-
pub rails_version: String,
11+
#[clap(long, short = 'r')]
12+
pub rails_version: Option<String>,
1313
#[clap(long)]
1414
pub rebuild: bool,
1515

@@ -54,10 +54,8 @@ mod tests {
5454
let m = Cli::command().get_matches_from(vec!["rails-new", "my_app"]);
5555

5656
let ruby_version = m.get_one::<String>("ruby_version").unwrap();
57-
let rails_version = m.get_one::<String>("rails_version").unwrap();
5857

5958
assert_eq!(ruby_version, "3.4.1");
60-
assert_eq!(rails_version, "8.0.1");
6159

6260
Ok(())
6361
}

0 commit comments

Comments
 (0)