1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! Porting defaults

// extern crate lazy_static;

use std::env;
use std::fs;
use std::path;
use std::process;

/// A Platform details a Docker and cargo build target configuration for building Rust applications
pub struct Platform {
    pub image_tag : String,
    pub target : String
}

lazy_static::lazy_static! {
    pub static ref PLATFORMS : Vec<Platform> = vec![
    Platform{
        image_tag: "x86_64-unknown-linux-gnu".to_string(),
        target: "x86_64-unknown-linux-gnu".to_string()
    },
    Platform{
        image_tag: "x86_64-unknown-linux-gnu".to_string(),
        target: "i686-unknown-linux-gnu".to_string()
    },
    Platform{
        image_tag: "arm-unknown-linux-gnueabi".to_string(),
        target: "arm-unknown-linux-gnueabi".to_string()
    }
    ];
}

/// PortConfig parameterizes a port job
pub struct PortConfig {
    pub image : String,
    pub is_release : bool,
    pub is_verbose : bool
}

/// Build a default PortConfig
pub fn new_portconfig() -> PortConfig {
    PortConfig{
        image: "mcandre/remy".to_string(),
        is_release: true,
        is_verbose: false
    }
}

/// Supply the standard executable suffix for a given target environment
pub fn executable_suffix(target : &str) -> &str {
    if target.contains("windows") {
        ".exe"
    } else {
        ""
    }
}

/// Generate application ports based on a PortConfig and a collection of binary basenames
pub fn port(portconfig : &PortConfig, binaries : Vec<String>) {
    for binary in binaries {
        for platform in PLATFORMS.iter() {
            let target : &str = &platform.target;
            let tagged_image : &str = &format!("{}:{}", portconfig.image, platform.image_tag);

            let cur_dir_canonical_pathbuf : path::PathBuf = env::current_dir()
                .unwrap()
                .as_path()
                .canonicalize()
                .unwrap();

            let cur_dir_canonical_str : &str = cur_dir_canonical_pathbuf
                .as_path()
                .to_str()
                .unwrap();

            let mount_join : &str = &format!(
                "{}:/src",
                cur_dir_canonical_str
            );

            let release_flag = if portconfig.is_release {
                " --release"
            } else {
                ""
            };

            let build_command : &str = &format!(
                "cd /src && cargo build --target {} --bin {}{}",
                target,
                binary,
                release_flag
            );

            let command_bare : &mut process::Command = &mut process::Command::new("docker");

            let command : &mut process::Command = command_bare
                .args(&[
                    "run",
                    "--rm",
                    "-v", mount_join,
                    tagged_image,
                    "sh", "-c", build_command
                ]);

            if portconfig.is_verbose {
                println!("{:?}", command);
            }

            assert!(
                command
                    .status()
                    .unwrap()
                    .success()
            );

            let build_mode = if portconfig.is_release {
                "release"
            } else {
                "debug"
            };

            let binary_filename : &str = &format!("{}{}", binary, executable_suffix(target));

            let port_path = path::Path::new("target")
                .join(target)
                .join(build_mode)
                .join(binary_filename);

            let bin_target = path::Path::new("target")
                .join("bin")
                .join(target);

            let merged_port_path = bin_target
                .join(binary_filename);

            fs::create_dir_all(bin_target)
                .unwrap();
            fs::copy(port_path, merged_port_path)
                .unwrap();
        }
    }
}