-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbuild.rs
151 lines (136 loc) · 4.81 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
// Copyright (c) 2024 Ronan LE MEILLAT for SCTG Development
//
// This file is part of the SCTGDesk project.
//
// SCTGDesk is free software: you can redistribute it and/or modify
// it under the terms of the Affero General Public License version 3 as
// published by the Free Software Foundation.
//
// SCTGDesk is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// Affero General Public License for more details.
//
// You should have received a copy of the Affero General Public License
// along with SCTGDesk. If not, see <https://www.gnu.org/licenses/agpl-3.0.html>.
use sqlx::{Connection, Executor, SqliteConnection};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::str;
#[derive(Debug, Serialize, Deserialize)]
pub struct PackageJson {
name: String,
private: Option<bool>,
version: String,
#[serde(rename = "type")]
type_: Option<String>,
scripts: HashMap<String, String>,
dependencies: HashMap<String, String>,
devDependencies: HashMap<String, String>,
}
impl PackageJson {
pub fn new() -> Self {
Self {
name: String::new(),
private: None,
version: String::new(),
type_: None,
scripts: HashMap::new(),
dependencies: HashMap::new(),
devDependencies: HashMap::new(),
}
}
pub fn set_version(&mut self, version: &str) {
self.version = version.to_string();
}
}
#[tokio::main]
async fn main() {
let db_path = env::var("DATABASE_URL").unwrap_or("sqlite://db_v2.sqlite3".to_string());
println!("cargo:rerun-if-changed=webconsole");
let data = fs::read_to_string("./webconsole/package.json").unwrap();
let mut package: PackageJson = serde_json::from_str(&data).unwrap();
// Construit le chemin du fichier dans le répertoire temporaire
let tmp_dir = env::var("TMP")
.or_else(|_| env::var("TEMP"))
.or_else(|_| env::var("TMPDIR"))
.unwrap_or_else(|_| "/tmp".to_string());
let mut path = PathBuf::from(tmp_dir);
path.push("version-8659B48F-5726-433D-BEC2-C7042FE9D93B.txt");
// Lit la version à partir du fichier
let version =
fs::read_to_string(&path).unwrap_or_else(|_| env::var("CARGO_PKG_VERSION").unwrap());
package.set_version(&version);
let serialized = serde_json::to_string_pretty(&package).unwrap();
fs::write("./webconsole/package.json", serialized).unwrap();
let is_windows = cfg!(target_os = "windows");
let (command, install_args, build_args) = if is_windows {
("cmd.exe", &["/C", "npm install --force"], &["/C", "npm run build"])
} else {
("npm", &["install", "--force"], &["run", "build"])
};
// Install npm dependencies for webconsole
let output = Command::new(command)
.current_dir("webconsole")
.args(install_args)
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Failed to install npm dependencies: {}{}",
str::from_utf8(&output.stdout).unwrap_or(""),
str::from_utf8(&output.stderr).unwrap_or("")
);
// Build webconsole
let output = Command::new(command)
.current_dir("webconsole")
.args(build_args)
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Failed to build webconsole: {}{}",
str::from_utf8(&output.stdout).unwrap_or(""),
str::from_utf8(&output.stderr).unwrap_or("")
);
// Install npm dependencies for rapidoc
let output = Command::new(command)
.current_dir("rapidoc")
.args(install_args)
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Failed to install npm dependencies: {}{}",
str::from_utf8(&output.stdout).unwrap_or(""),
str::from_utf8(&output.stderr).unwrap_or("")
);
// Build rapidoc
let output = Command::new(command)
.current_dir("rapidoc")
.args(build_args)
.output()
.expect("Failed to execute command");
assert!(
output.status.success(),
"Failed to build rapidoc: {}{}",
str::from_utf8(&output.stdout).unwrap_or(""),
str::from_utf8(&output.stderr).unwrap_or("")
);
// Delete test data if it exists
let mut conn = SqliteConnection::connect(&format!("{}", db_path))
.await
.expect("Failed to open database");
conn.execute(
r#"
DELETE FROM peer WHERE guid = x'95CC7775BA37481DAD7214A4F6CE5A94';
PRAGMA journal_mode=DELETE;
"#
)
.await
.expect("Failed to delete test data");
}