forked from grasegger/rust-web-extension-talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
207 lines (174 loc) · 6.83 KB
/
build.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
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
// use wasm_pack::command::run_wasm_pack as wasm_pack;
use std::path::{Path, PathBuf};
use std::{
error::Error,
fs::{self, File},
};
use std::process::Command;
use wasm_pack::command::build::Build;
use wasm_pack::command::build::BuildOptions;
use wasm_pack::command::build::Target;
use std::fs::OpenOptions;
use std::io::prelude::*;
use json::{object, stringify_pretty, JsonValue};
use toml::Value;
pub fn main() {
create_manifest().expect("There was an error creating the manifest file.");
copy_artifacts().expect("I was unable to copy the sources from the artifacts folder.");
install_yarn().expect("I was not able to execute yarn in the pkg folder correctly.");
}
fn create_manifest() -> Result<(), Box<dyn Error>> {
let cargo_file_content = read_cargo_file("Cargo.toml".to_string())?;
let parsed_cargo_file = cargo_file_content.parse::<Value>()?;
let gecko_id = object! {
gecko: object!{
id: parsed_cargo_file["package"]["metadata"]["webextension"]["gecko_id"].as_str()
}
};
let mut manifest_json = object! {
manifest_version: 2,
version: parsed_cargo_file["package"]["version"].as_str(),
description: parsed_cargo_file["package"]["description"].as_str(),
author: parsed_cargo_file["package"]["authors"][0].as_str(),
name: parsed_cargo_file["package"]["metadata"]["webextension"]["extension_name"].as_str(),
content_scripts: Vec::<JsonValue>::new(),
background: object! {
scripts: Vec::<JsonValue>::new(),
},
web_accessible_resources: Vec::<JsonValue>::new(),
permissions: Vec::<JsonValue>::new(),
applications: gecko_id.clone(),
browser_specific_settings: gecko_id,
};
let artifacts = Path::new("./artifacts");
for script in fs::read_dir(artifacts)? {
let script = script?;
let path = script.path().to_str().unwrap().replace("./artifacts/", "");
manifest_json["web_accessible_resources"].push(path)?;
}
for permission in parsed_cargo_file["package"]["metadata"]["webextension"]["permissions"]
.as_array()
.unwrap()
{
manifest_json["permissions"].push(permission.as_str())?;
}
for content_script in parsed_cargo_file["package"]["metadata"]["webextension"]
["content_scripts"]
.as_array()
.unwrap()
{
let mut script = object! {
matches: Vec::<JsonValue>::new(),
js: Vec::<JsonValue>::new(),
css: Vec::<JsonValue>::new(),
};
for site_match in content_script["matches"].as_array().unwrap() {
script["matches"].push(site_match.as_str())?;
}
for js in content_script["js"].as_array().unwrap() {
let source = js.as_str().unwrap();
let extension = source.split(".").last().unwrap();
if extension == "toml" {
let package_name = get_package_name_from_path(source.into()).unwrap();
let source_folder = source.split("/Cargo.toml").next().unwrap();
build_script(package_name.clone(), Path::new(source_folder).into());
script["js"].push(format!("{}.js", &package_name))?;
manifest_json["web_accessible_resources"]
.push(format!("{}_bg.wasm", &package_name))?;
} else {
script["js"].push(source)?;
}
}
manifest_json["content_scripts"].push(script)?;
}
for background_script in parsed_cargo_file["package"]["metadata"]["webextension"]["background"]
.as_array()
.unwrap()
{
for js in background_script["js"].as_array().unwrap() {
let source = js.as_str().unwrap();
let extension = source.split(".").last().unwrap();
if extension == "toml" {
let package_name = get_package_name_from_path(source.into()).unwrap();
let source_folder = source.split("/Cargo.toml").next().unwrap();
build_script(package_name.clone(), Path::new(source_folder).into());
manifest_json["background"]["scripts"].push(format!("{}.js", &package_name))?;
manifest_json["web_accessible_resources"]
.push(format!("{}_bg.wasm", &package_name))?;
} else {
manifest_json["background"]["scripts"].push(source)?;
}
}
}
let pkg_path = Path::new("pkg");
if !pkg_path.exists() {
fs::create_dir(pkg_path)?;
}
let manifest_path = Path::new("pkg/manifest.json");
write_json(manifest_json, manifest_path)?;
Ok(())
}
fn get_package_name_from_path(path: String) -> Result<String, Box<dyn Error>> {
let cargo_contents_string = read_cargo_file(path)?;
let cargo_contents_toml = cargo_contents_string.parse::<Value>()?;
Ok(cargo_contents_toml["package"]["name"]
.as_str()
.unwrap()
.to_string())
}
fn read_cargo_file(path: String) -> Result<String, Box<dyn Error>> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
fn write_json(json: JsonValue, path: &Path) -> Result<(), Box<dyn Error>> {
let mut file = File::create(path)?;
file.write_all(stringify_pretty(json, 2).as_bytes())?;
Ok(())
}
fn build_script(name: String, path: PathBuf) {
let build_opts = BuildOptions {
path: Some(path),
disable_dts: true,
target: Target::NoModules,
out_name: Some(name.clone()),
out_dir: "../../pkg".to_string(),
..Default::default()
};
let mut command = Build::try_from_opts(build_opts).unwrap();
command.run().unwrap();
let formated_dest_path = format!("pkg/{}.js", name);
let js_dest_path = Path::new(&formated_dest_path);
let mut old_js_file_content = fs::read_to_string(js_dest_path).unwrap();
old_js_file_content += &format!("wasm_bindgen(browser.runtime.getURL('{}_bg.wasm'));", name);
let mut js_file = OpenOptions::new()
.write(true)
.append(false)
.open(js_dest_path)
.unwrap();
write!(js_file, "(function () {{ {} }})()", old_js_file_content).unwrap();
}
fn copy_artifacts() -> Result<(), std::io::Error> {
let path = Path::new("./artifacts");
for artifact in fs::read_dir(path)? {
let artifact = artifact?;
let path = artifact.path();
let target = String::from(format!(
"./pkg/{}",
path.to_str().unwrap().replace("./artifacts/", "")
));
println!("{}", target);
println!("{:?}", path);
fs::copy(path, target)?;
}
Ok(())
}
fn install_yarn() -> Result<(), Box<dyn Error>> {
let mut command = Command::new("yarn");
command
.current_dir("./pkg")
.output()
.expect("Error while installing the yarn dependencies.");
Ok(())
}