This repository has been archived by the owner on Nov 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
assemblyscript.rs
80 lines (63 loc) · 2.41 KB
/
assemblyscript.rs
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
use cli::{package, scaffold::Scaffold};
use config_files::Build;
use error::DefaultResult;
use std::{
fs::{self, OpenOptions},
io::Write,
path::Path,
};
use util;
pub const TSCONFIG_FILE_NAME: &str = "tsconfig.json";
pub const TYPESCRIPT_FILE_NAME: &str = "index.ts";
pub struct AssemblyScriptScaffold {
build_template: Build,
}
impl AssemblyScriptScaffold {
pub fn new() -> AssemblyScriptScaffold {
AssemblyScriptScaffold {
build_template: Build::with_artifact("module.wasm")
.cmd(
"./node_modules/assemblyscript/bin/asc",
&["index.ts", "-b", "module.wasm", "--transform", "./node_modules/hdk-assemblyscript/transforms"],
),
}
}
}
impl Scaffold for AssemblyScriptScaffold {
fn gen<P: AsRef<Path>>(&self, base_path: P) -> DefaultResult<()> {
fs::create_dir_all(&base_path)?;
// use npm to initialise a nodejs project
util::run_cmd(
base_path.as_ref().to_path_buf(),
"npm".into(),
vec![
"init".to_owned(),
"-y".to_owned(),
],
)?;
// add hdk-assemblyscript as a dependency
util::run_cmd(
base_path.as_ref().to_path_buf(),
"npm".into(),
vec![
"install".to_owned(),
"--save".to_owned(),
"holochain/hdk-assemblyscript".to_owned(),
],
)?;
// create a index.ts file
let typescript_file_path = base_path.as_ref().join(TYPESCRIPT_FILE_NAME);
let mut typescript_file = OpenOptions::new().write(true).create(true).open(typescript_file_path)?;
let js_starter = include_str!("assemblyscript/index-ts-starter.ts");
typescript_file.write_all(js_starter.as_bytes())?;
// create a tsconfig.json file
let tsconfig_file_path = base_path.as_ref().join(TSCONFIG_FILE_NAME);
let mut tsconfig_file = OpenOptions::new().write(true).create(true).open(tsconfig_file_path)?;
let tsconfig_json = include_str!("assemblyscript/tsconfig.json");
tsconfig_file.write_all(tsconfig_json.as_bytes())?;
// create and fill in a build file appropriate for AssemblyScript
let build_file_path = base_path.as_ref().join(package::BUILD_CONFIG_FILE_NAME);
self.build_template.save_as(build_file_path)?;
Ok(())
}
}