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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::{io::Write, str::FromStr};
use codespan_reporting::diagnostic::Diagnostic;
use crate::{
    dockerfile::{Dockerfile, Image, Instruction, ResolvedDockerfile, ResolvedParent, Run},
    imagegen::{self, BuildPlan, MergeNode, NodeId},
    logic::{self, Clause, IRTerm, Literal, Predicate},
    modusfile::{self, Modusfile},
    sld::{self, ClauseId, ResolutionError, SLDResult, Tree},
};
use crate::imagegen::BuildNode;
pub fn render_tree<W: Write>(clauses: &Vec<Clause>, sld_result: SLDResult, output: &mut W) {
    
    let g = sld_result.tree.to_graph(clauses);
    dot::render(&g, output).unwrap()
}
pub fn transpile(
    mf: Modusfile,
    query: modusfile::Expression,
) -> Result<Dockerfile<ResolvedParent>, Vec<Diagnostic<()>>> {
    let build_plan = imagegen::plan_from_modusfile(mf, query)?;
    Ok(plan_to_docker(&build_plan))
}
fn plan_to_docker(plan: &BuildPlan) -> ResolvedDockerfile {
    let topological_order = plan.topological_order();
    let mut instructions = topological_order
        .into_iter()
        .map(|node_id| {
            use crate::dockerfile::*;
            let node = &plan.nodes[node_id];
            let str_id = format!("n_{}", node_id);
            match node {
                BuildNode::From {
                    image_ref,
                    display_name: _,
                } => vec![Instruction::From(From {
                    parent: ResolvedParent::Image(Image::from_str(image_ref).unwrap()),
                    alias: Some(str_id),
                })],
                BuildNode::Run {
                    parent,
                    command,
                    cwd,
                    additional_envs,
                } => {
                    let mut instructions = vec![Instruction::From(From {
                        parent: ResolvedParent::Stage(format!("n_{}", parent)),
                        alias: Some(str_id),
                    })];
                    for (k, v) in additional_envs.iter() {
                        instructions.push(Instruction::Env(Env(format!("{}={}", k, v))));
                    }
                    instructions.push(Instruction::Run(Run(if cwd.is_empty() {
                        command.to_owned()
                    } else {
                        format!("cd {:?} || exit 1; {}", cwd, command)
                    })));
                    instructions
                }
                BuildNode::CopyFromImage {
                    parent,
                    src_image,
                    src_path,
                    dst_path,
                } => vec![
                    Instruction::From(From {
                        parent: ResolvedParent::Stage(format!("n_{}", parent)),
                        alias: Some(str_id),
                    }),
                    Instruction::Copy(Copy(format!(
                        "--from=n_{} {:?} {:?}", 
                        src_image, src_path, dst_path
                    ))),
                ],
                BuildNode::CopyFromLocal {
                    parent,
                    src_path,
                    dst_path,
                } => vec![
                    Instruction::From(From {
                        parent: ResolvedParent::Stage(format!("n_{}", parent)),
                        alias: Some(str_id),
                    }),
                    Instruction::Copy(Copy(format!("{:?} {:?}", src_path, dst_path))),
                ],
                BuildNode::SetWorkdir {
                    parent,
                    new_workdir,
                } => vec![
                    Instruction::From(From {
                        parent: ResolvedParent::Stage(format!("n_{}", parent)),
                        alias: Some(str_id),
                    }),
                    Instruction::Workdir(Workdir(new_workdir.to_string())),
                ],
                BuildNode::SetEntrypoint {
                    parent,
                    new_entrypoint,
                } => vec![
                    Instruction::From(From {
                        parent: ResolvedParent::Stage(format!("n_{}", parent)),
                        alias: Some(str_id),
                    }),
                    Instruction::Entrypoint(format!("{:?}", new_entrypoint)),
                ],
                BuildNode::SetLabel {
                    parent,
                    label,
                    value,
                } => vec![
                    Instruction::From(From {
                        parent: ResolvedParent::Stage(format!("n_{}", parent)),
                        alias: Some(str_id),
                    }),
                    Instruction::Label(label.to_owned(), value.to_owned()),
                ],
                BuildNode::Merge(MergeNode { parent, operations }) => {
                    let mut insts = Vec::new();
                    insts.push(Instruction::From(From {
                        parent: ResolvedParent::Stage(format!("n_{}", parent)),
                        alias: Some(str_id),
                    }));
                    for op in operations {
                        use imagegen::MergeOperation;
                        match op {
                            MergeOperation::Run {
                                command,
                                cwd,
                                additional_envs,
                            } => {
                                for (k, v) in additional_envs.iter() {
                                    insts.push(Instruction::Env(Env(format!("{}={}", k, v))));
                                }
                                insts.push(Instruction::Run(Run(if cwd.is_empty() {
                                    command.to_owned()
                                } else {
                                    format!("cd {:?} || exit 1; {}", cwd, command)
                                })));
                            }
                            MergeOperation::CopyFromLocal { src_path, dst_path } => {
                                insts.push(Instruction::Copy(Copy(format!(
                                    "{:?} {:?}",
                                    src_path, dst_path
                                ))));
                            }
                            MergeOperation::CopyFromImage {
                                src_image,
                                src_path,
                                dst_path,
                            } => {
                                insts.push(Instruction::Copy(Copy(format!(
                                    "--from=n_{} {:?} {:?}",
                                    src_image, src_path, dst_path
                                ))));
                            }
                        }
                    }
                    insts
                }
                BuildNode::SetEnv { parent, key, value } => vec![
                    Instruction::From(From {
                        parent: ResolvedParent::Stage(format!("n_{}", parent)),
                        alias: Some(str_id),
                    }),
                    Instruction::Env(Env(format!("{}={}", key, value))),
                ],
                BuildNode::AppendEnvValue { parent, key, value } => {
                    todo!()
                }
            }
        })
        .flatten()
        .collect::<Vec<_>>();
    if plan.outputs.len() > 1 {
        use crate::dockerfile::{From, Run};
        instructions.push(Instruction::From(From {
            parent: ResolvedParent::Stage("busybox".to_owned()),
            alias: Some("force_multioutput".to_owned()),
        }));
        for o in plan.outputs.iter() {
            let k = format!("n_{}", o.node);
            instructions.push(Instruction::Run(Run(format!(
                "--mount=type=bind,from={},source=/,target=/mnt true",
                k,
            ))));
        }
    }
    Dockerfile(instructions)
}