Fork of daniellemaywood.uk/gleam — Wasm codegen work
6.3 kB
211 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2021 The Gleam contributors
3
4use crate::{
5 build::{Mode, Target},
6 manifest::Base16Checksum,
7};
8
9use camino::{Utf8Path, Utf8PathBuf};
10
11pub const ARTEFACT_DIRECTORY_NAME: &str = "_gleam_artefacts";
12
13#[derive(Debug, Clone)]
14pub struct ProjectPaths {
15 root: Utf8PathBuf,
16}
17
18impl ProjectPaths {
19 pub fn new(root: Utf8PathBuf) -> Self {
20 Self { root }
21 }
22
23 pub fn at_filesystem_root() -> Self {
24 let path = if cfg!(target_family = "windows") {
25 r"C:\"
26 } else {
27 "/"
28 };
29
30 Self::new(Utf8PathBuf::from(path))
31 }
32
33 pub fn root(&self) -> &Utf8Path {
34 &self.root
35 }
36
37 pub fn root_config(&self) -> Utf8PathBuf {
38 self.root.join("gleam.toml")
39 }
40
41 pub fn readme(&self) -> Utf8PathBuf {
42 self.root.join("README.md")
43 }
44
45 pub fn manifest(&self) -> Utf8PathBuf {
46 self.root.join("manifest.toml")
47 }
48
49 pub fn src_directory(&self) -> Utf8PathBuf {
50 self.root.join("src")
51 }
52
53 pub fn test_directory(&self) -> Utf8PathBuf {
54 self.root.join("test")
55 }
56
57 pub fn dev_directory(&self) -> Utf8PathBuf {
58 self.root.join("dev")
59 }
60
61 pub fn build_directory(&self) -> Utf8PathBuf {
62 self.root.join("build")
63 }
64
65 pub fn build_packages_directory(&self) -> Utf8PathBuf {
66 self.build_directory().join("packages")
67 }
68
69 pub fn build_git_directory(&self) -> Utf8PathBuf {
70 self.build_directory().join("git")
71 }
72
73 pub fn build_git_repo(&self, name: &str) -> Utf8PathBuf {
74 self.build_git_directory().join(name)
75 }
76
77 pub fn build_packages_toml(&self) -> Utf8PathBuf {
78 self.build_packages_directory().join("packages.toml")
79 }
80
81 pub fn build_packages_package(&self, package_name: &str) -> Utf8PathBuf {
82 self.build_packages_directory().join(package_name)
83 }
84
85 // build_deps_package_config
86 pub fn build_packages_package_config(&self, package_name: &str) -> Utf8PathBuf {
87 self.build_packages_package(package_name).join("gleam.toml")
88 }
89
90 pub fn build_export_hex_tarball(&self, package_name: &str, version: &str) -> Utf8PathBuf {
91 self.build_directory()
92 .join(format!("{package_name}-{version}.tar"))
93 }
94
95 pub fn build_directory_for_mode(&self, mode: Mode) -> Utf8PathBuf {
96 self.build_directory().join(mode.to_string())
97 }
98
99 pub fn erlang_shipment_directory(&self) -> Utf8PathBuf {
100 self.build_directory().join("erlang-shipment")
101 }
102
103 pub fn build_documentation_directory(&self, package: &str) -> Utf8PathBuf {
104 self.build_directory_for_mode(Mode::Dev)
105 .join("docs")
106 .join(package)
107 }
108
109 pub fn build_directory_for_target(&self, mode: Mode, target: Target) -> Utf8PathBuf {
110 let target = match target {
111 Target::Erlang => "erlang",
112 Target::JavaScript => "javascript",
113 };
114 self.build_directory_for_mode(mode).join(target)
115 }
116
117 /// Note this uses the "application name", not the name of this package.
118 /// This is because in BEAM applications one can specify an application
119 /// name that is not the same as the Hex package name. Ideally we would
120 /// always use the package name, but the BEAM runtime knows nothing
121 /// about packages, only applications, so it will look on the filesystem
122 /// for the application name when loading it.
123 pub fn build_directory_for_package(
124 &self,
125 mode: Mode,
126 target: Target,
127 application_name: &str,
128 ) -> Utf8PathBuf {
129 self.build_directory_for_target(mode, target)
130 .join(application_name)
131 }
132
133 pub fn build_packages_ebins_glob(&self, mode: Mode, target: Target) -> Utf8PathBuf {
134 self.build_directory_for_package(mode, target, "*")
135 .join("ebin")
136 }
137
138 /// A path to a special file that contains the version of gleam
139 /// that last built the artefacts along with build-impacting
140 /// configuration, such as whether to generate source maps. If
141 /// this file does not match the current version of gleam we
142 /// will rebuild from scratch
143 pub fn build_configuration_fingerprint(&self, mode: Mode, target: Target) -> Utf8PathBuf {
144 self.build_directory_for_target(mode, target)
145 .join("fingerprint")
146 }
147
148 /// The path to the source gleam.toml file for a path dependency.
149 pub fn path_dependency_gleam_toml_path(&self, dependency_path: &Utf8Path) -> Utf8PathBuf {
150 self.root().join(dependency_path).join("gleam.toml")
151 }
152
153 pub fn dependency_gleam_toml_fingerprint_path(&self, dependency_name: &str) -> Utf8PathBuf {
154 self.build_packages_directory()
155 .join(format!("{}.config_fingerprint", dependency_name))
156 }
157}
158
159pub fn global_package_cache_package_tarball(checksum: &Base16Checksum) -> Utf8PathBuf {
160 global_packages_cache().join(format!("{}.tar", checksum.base_16_encoded_string()))
161}
162
163pub fn global_hexpm_oauth_credentials_path() -> Utf8PathBuf {
164 global_hexpm_cache().join("credentials.toml")
165}
166
167pub fn global_hexpm_legacy_credentials_path() -> Utf8PathBuf {
168 global_hexpm_cache().join("credentials")
169}
170
171fn global_hexpm_cache() -> Utf8PathBuf {
172 default_global_gleam_cache().join("hex").join("hexpm")
173}
174
175fn global_packages_cache() -> Utf8PathBuf {
176 global_hexpm_cache().join("packages")
177}
178
179pub fn default_global_gleam_cache() -> Utf8PathBuf {
180 Utf8PathBuf::from_path_buf(
181 dirs_next::cache_dir()
182 .expect("Failed to determine user cache directory")
183 .join("gleam"),
184 )
185 .expect("Non Utf8 Path")
186}
187
188pub fn unnest(within: &Utf8Path) -> Utf8PathBuf {
189 let mut path = Utf8PathBuf::new();
190 for _ in within {
191 path = path.join("..")
192 }
193 path
194}
195
196#[test]
197fn paths() {
198 assert!(default_global_gleam_cache().ends_with("gleam"));
199
200 assert!(global_packages_cache().ends_with("hex/hexpm/packages"));
201
202 assert!(
203 global_package_cache_package_tarball(&Base16Checksum(vec![0, 0, 0, 0]))
204 .ends_with("hex/hexpm/packages/00000000.tar")
205 );
206
207 assert!(
208 global_package_cache_package_tarball(&Base16Checksum(vec![0x3A, 0x21, 0xF4]))
209 .ends_with("hex/hexpm/packages/3A21F4.tar")
210 );
211}