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
use clap::Clap;
use inflections::Inflect;
use std::fs::{create_dir, read_dir, File};
use std::io::{Error, Write};
use std::process::Command;
#[derive(Clap)]
pub struct Init {
name: String,
}
impl Init {
pub fn main(&self) {
for entry in read_dir(std::env::current_dir().unwrap()).unwrap() {
match entry.unwrap().file_name().to_str().unwrap() {
".polyhorn" => {}
name => {
eprintln!(
"error: can't initialize new Polyhorn project due to existing file: {:?}",
name
);
std::process::abort();
}
}
}
self.write_assets().unwrap();
self.write_build().unwrap();
self.write_cargo().unwrap();
self.write_gitignore().unwrap();
self.write_polyhorn().unwrap();
let _ = create_dir("src");
self.write_lib().unwrap();
assert!(Command::new("cargo")
.arg("generate-lockfile")
.spawn()
.unwrap()
.wait()
.unwrap()
.success());
}
fn write_assets(&self) -> Result<(), Error> {
let _ = create_dir("assets");
File::create("assets/.gitkeep")?;
Ok(())
}
fn write_cargo(&self) -> Result<(), Error> {
let mut file = File::create("Cargo.toml").unwrap();
file.write_fmt(format_args!(
include_str!("../../template/Cargo.toml.tmpl"),
name = self.name,
version = env!("CARGO_PKG_VERSION"),
))
}
fn write_gitignore(&self) -> Result<(), Error> {
let mut file = File::create(".gitignore").unwrap();
file.write_fmt(format_args!(include_str!("../../template/.gitignore.tmpl"),))
}
fn write_polyhorn(&self) -> Result<(), Error> {
let mut file = File::create("Polyhorn.toml").unwrap();
file.write_fmt(format_args!(
include_str!("../../template/Polyhorn.toml.tmpl"),
title = self.name.to_title_case(),
pascal = self.name.to_pascal_case(),
snake = self.name.to_snake_case(),
))
}
fn write_build(&self) -> Result<(), Error> {
let mut file = File::create("build.rs").unwrap();
file.write_fmt(format_args!(include_str!("../../template/build.rs.tmpl")))
}
fn write_lib(&self) -> Result<(), Error> {
let mut file = File::create("src/lib.rs").unwrap();
file.write_fmt(format_args!(include_str!("../../template/src/lib.rs.tmpl")))
}
}