Manifest.vala 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. using Invercargill;
  2. using Invercargill.Mapping;
  3. using Invercargill.DataStructures;
  4. namespace Usm {
  5. public errordomain ManifestError {
  6. MISSING_FIELD,
  7. INVALID_VERSION,
  8. INVALID_LICENCE_CATEGORY,
  9. INVALID_RESOURCE_TYPE,
  10. INVALID_FILE_TYPE,
  11. INVALID_REMOVE_TYPE,
  12. INVALID_INSTALL_TYPE,
  13. INVALID_PACKAGE,
  14. INVALID_PATH_BASE,
  15. INVALID_FILE_PATH,
  16. INVALID_FLAG,
  17. /** A dataPackage manifest defines a lifecycle executable (only acquire is permitted) */
  18. DATA_PACKAGE_WITH_EXECUTABLE,
  19. /** A "depends" phase is neither a flat array of refs nor an array of candidate groups (mixed forms, over-nested arrays or non-string entries) */
  20. INVALID_DEPENDENCIES,
  21. /** A candidate group in a "depends" phase is empty */
  22. EMPTY_DEPENDENCY_GROUP
  23. }
  24. public class Manifest {
  25. public string name { get; set; }
  26. public string summary { get; set; }
  27. public Version version { get; set; }
  28. public Vector<Licence> licences { get; set; }
  29. public Dictionary<ResourceRef, ManifestFile> provides { get; set; }
  30. public Dependencies dependencies { get; set; }
  31. public Executables executables { get; set; }
  32. public Set<ManifestFlag> flags { get; set; }
  33. public string? markdown_path { get; set; }
  34. public string? url { get; set; }
  35. public Vector<string>? screenshot_paths { get; set; }
  36. public string? icon_path { get; set; }
  37. public string? metainfo_path { get; set; }
  38. public Git? git { get; set; }
  39. public Properties? extra_properties { get; set; }
  40. /**
  41. * Whether the manifest declares {@link ManifestFlag.DATA_PACKAGE}:
  42. * a package with no lifecycle executables whose {@link provides}
  43. * are copied directly from the source tree at install time.
  44. */
  45. public bool is_data_package {
  46. get {
  47. return flags != null && flags.contains(ManifestFlag.DATA_PACKAGE);
  48. }
  49. }
  50. public static PropertyMapper<Manifest> get_mapper() {
  51. return PropertyMapper.build_for<Manifest>(cfg => {
  52. cfg.map<string>("name", o => o.name, (o, v) => o.name = v);
  53. cfg.map<string>("version", o => o.version.to_string(), (o, v) => o.version = new Version.from_string(v));
  54. cfg.map<string>("summary", o => o.summary, (o, v) => o.summary = v);
  55. cfg.map_property_groups_with<Licence>("licences", o => o.licences, (o, v) => o.licences = v.to_vector(), Licence.get_mapper());
  56. cfg.map<Properties>("provides", o => o.map_from_provides_dict(), (o, v) => o.build_provides_dict(v));
  57. cfg.map_properties_with<Dependencies>("depends", o => o.dependencies, (o, v) => o.dependencies = v, Dependencies.get_mapper());
  58. cfg.map_properties_with<Executables>("execs", o => o.executables, (o, v) => o.executables = v, Executables.get_mapper())
  59. .undefined_when(o => o.executables == null)
  60. .when_undefined(o => o.executables = new Executables());
  61. cfg.map_many<string>("flags", o => o.flags.select<string>(f => f.to_string()), (o, v) => o.flags = v.attempt_select<ManifestFlag>(f => ManifestFlag.from_string(f)).to_set());
  62. cfg.map<string>("md", o => o.markdown_path, (o, v) => o.markdown_path = v)
  63. .undefined_when(o => o.markdown_path == null)
  64. .when_undefined(o => o.markdown_path = null);
  65. cfg.map<string>("url", o => o.url, (o, v) => o.url = v)
  66. .undefined_when(o => o.url == null)
  67. .when_undefined(o => o.url = null);
  68. cfg.map_many<string>("screenshots", o => o.screenshot_paths, (o, v) => o.screenshot_paths = v.to_vector())
  69. .undefined_when(o => o.screenshot_paths == null)
  70. .when_undefined(o => o.screenshot_paths = null);
  71. cfg.map<string>("icon", o => o.icon_path, (o, v) => o.icon_path = v)
  72. .undefined_when(o => o.icon_path == null)
  73. .when_undefined(o => o.icon_path = null);
  74. cfg.map<string>("metainfo", o => o.metainfo_path, (o, v) => o.metainfo_path = v)
  75. .undefined_when(o => o.metainfo_path == null)
  76. .when_undefined(o => o.metainfo_path = null);
  77. // cfg.map_with<Git>("git", o => o.git, (o, v) => o.git = v, Git.get_mapper(), false);
  78. cfg.map<Properties>("extras", o => o.extra_properties, (o, v) => o.extra_properties = v)
  79. .undefined_when(o => o.extra_properties == null)
  80. .when_undefined(o => o.extra_properties = null);
  81. cfg.set_constructor(() => new Manifest());
  82. });
  83. }
  84. public Manifest.from_file(string path) throws Error {
  85. var element = new InvercargillJson.JsonElement.from_file(path);
  86. Manifest.get_mapper().map_into(this, element.as<Invercargill.Properties>());
  87. validate();
  88. }
  89. public Manifest.from_package(string path) throws Error {
  90. var archive = new Archive.Read();
  91. archive.support_format_tar();
  92. archive.support_filter_xz();
  93. var result = archive.open_filename(path, 10240);
  94. if(result != Archive.Result.OK) {
  95. throw new ManifestError.INVALID_PACKAGE("Could not read archive");
  96. }
  97. unowned Archive.Entry entry;
  98. while(archive.next_header(out entry) == Archive.Result.OK) {
  99. var path_name = entry.pathname();
  100. if(path_name != "./MANIFEST.usm") {
  101. continue;
  102. }
  103. var manifest_blob = new ByteComposition();
  104. uint8[] buffer;
  105. Posix.off_t offset;
  106. while (archive.read_data_block (out buffer, out offset) == Archive.Result.OK) {
  107. manifest_blob.append_byte_array(buffer[offset:]);
  108. }
  109. var element = new InvercargillJson.JsonElement.from_string(manifest_blob.to_raw_string());
  110. Manifest.get_mapper().map_into(this, element.as<Invercargill.Properties>());
  111. validate();
  112. return;
  113. }
  114. throw new ManifestError.INVALID_PACKAGE("MANIFEST.usm not found within archive");
  115. }
  116. /**
  117. * Validates cross-field constraints that parsing alone cannot catch.
  118. *
  119. * Currently enforces the {@link ManifestFlag.DATA_PACKAGE} contract:
  120. * a data package must not define any lifecycle executable (build,
  121. * install, rebuild, test, remove or postInstall); the acquire
  122. * executable remains honoured. Throws
  123. * {@link ManifestError.DATA_PACKAGE_WITH_EXECUTABLE} naming every
  124. * offending executable otherwise.
  125. */
  126. public void validate() throws Error {
  127. if(!is_data_package) {
  128. return;
  129. }
  130. var defined = new Vector<string>();
  131. if(executables.build != null) {
  132. defined.add("build");
  133. }
  134. if(executables.install != null) {
  135. defined.add("install");
  136. }
  137. if(executables.rebuild != null) {
  138. defined.add("rebuild");
  139. }
  140. if(executables.test != null) {
  141. defined.add("test");
  142. }
  143. if(executables.remove != null) {
  144. defined.add("remove");
  145. }
  146. if(executables.post_install != null) {
  147. defined.add("postInstall");
  148. }
  149. if(defined.any()) {
  150. throw new ManifestError.DATA_PACKAGE_WITH_EXECUTABLE(
  151. @"Manifest \"$name\" declares the dataPackage flag, but data packages cannot define lifecycle executables (found: $(string.joinv(", ", defined.to_array()))). Only the acquire executable is permitted."
  152. );
  153. }
  154. }
  155. private void build_provides_dict(Properties obj) throws Error {
  156. provides = new Dictionary<ResourceRef, ManifestFile>();
  157. var mapper = ManifestFile.get_mapper();
  158. foreach (var pair in obj) {
  159. ManifestFile file;
  160. if(pair.value.assignable_to<string>()) {
  161. file = new ManifestFile.from_string(pair.value.as<string>());
  162. }
  163. else {
  164. file = mapper.materialise(pair.value.as<Properties>());
  165. }
  166. // Validate the ManifestFile after creation
  167. file.validate();
  168. provides[new ResourceRef(pair.key)] = file;
  169. }
  170. }
  171. private Properties map_from_provides_dict() {
  172. var dict = new PropertyDictionary();
  173. var mapper = ManifestFile.get_mapper();
  174. foreach (var pair in provides) {
  175. try {
  176. // Handle special case: when pathBase is as-expected, path is empty, and type is reg
  177. // output string "as-expected" instead of full JSON object
  178. if(pair.value.path_base == ManifestFilePathBase.AS_EXPECTED &&
  179. pair.value.path == "" &&
  180. pair.value.file_type == ManifestFileType.REGULAR) {
  181. dict.set_native<string>(pair.key.to_string(), "as-expected");
  182. } else {
  183. dict.set_native<Properties>(pair.key.to_string(), mapper.map_from(pair.value));
  184. }
  185. }
  186. catch(Error e) {
  187. assert_not_reached();
  188. }
  189. }
  190. return dict;
  191. }
  192. /**
  193. * Whether package management-script output should stream to the
  194. * terminal instead of being silenced, requested via the USM_VERBOSE
  195. * environment variable (set by `usm --verbose`, inherited by nested
  196. * invocations and spawned scripts).
  197. */
  198. private static bool verbose_requested() {
  199. var value = Environment.get_variable("USM_VERBOSE");
  200. return value != null && value.length > 0;
  201. }
  202. /**
  203. * Strips the output-silencing and -piping flags when verbose output
  204. * was requested, so script output inherits the terminal unchanged.
  205. */
  206. private static SubprocessFlags verbose_flags(SubprocessFlags flags) {
  207. if(!verbose_requested()) {
  208. return flags;
  209. }
  210. var result = flags;
  211. if((result & SubprocessFlags.STDOUT_SILENCE) != 0) {
  212. result = result & ~SubprocessFlags.STDOUT_SILENCE;
  213. }
  214. if((result & SubprocessFlags.STDOUT_PIPE) != 0) {
  215. result = result & ~SubprocessFlags.STDOUT_PIPE;
  216. }
  217. if((result & SubprocessFlags.STDERR_SILENCE) != 0) {
  218. result = result & ~SubprocessFlags.STDERR_SILENCE;
  219. }
  220. return result;
  221. }
  222. public Subprocess run_build(string build_path, Paths paths, SubprocessFlags flags, ProgressDelegate? progress_delegate = null) throws Error {
  223. if(executables.build == null) {
  224. throw new ManifestError.MISSING_FIELD(@"Manifest \"$name\" defines no build executable");
  225. }
  226. // Handle SIMPLE_BUILD_ENVIRONMENT flag
  227. string original_working_dir = Environment.get_current_dir();
  228. string working_dir = original_working_dir;
  229. string effective_build_path = build_path;
  230. if (this.flags.contains(ManifestFlag.SIMPLE_BUILD_ENVIRONMENT)) {
  231. // Copy the full source tree to the build directory
  232. var source_dir = File.new_for_path(working_dir);
  233. var build_dir = File.new_for_path(build_path);
  234. // Copy all files from source to build directory
  235. copy_directory(source_dir, build_dir);
  236. // Use build directory as working directory
  237. working_dir = build_path;
  238. }
  239. var path = Path.build_filename(working_dir, executables.build);
  240. // Change to the working directory for subprocess execution
  241. Environment.set_current_dir(working_dir);
  242. paths.set_envs();
  243. // Check if NINJA_STYLE_PROGRESS flag is set and progress delegate is provided
  244. if (this.flags.contains(ManifestFlag.NINJA_STYLE_PROGRESS) && progress_delegate != null && !verbose_requested()) {
  245. // Set up subprocess to capture STDOUT for progress parsing
  246. var modified_flags = flags;
  247. // Ensure STDOUT is not silenced when we need to parse progress
  248. if ((modified_flags & SubprocessFlags.STDOUT_SILENCE) != 0) {
  249. modified_flags = modified_flags & ~SubprocessFlags.STDOUT_SILENCE;
  250. }
  251. modified_flags = modified_flags | SubprocessFlags.STDOUT_PIPE;
  252. var proc = new Subprocess.newv(new string[] { path, Paths.ensure_trailing_slash(effective_build_path) }, modified_flags);
  253. // Start a new thread to monitor STDOUT for progress information
  254. ThreadFunc<void> progress_thread_func = () => {
  255. try {
  256. var stdout_pipe = proc.get_stdout_pipe();
  257. if (stdout_pipe != null) {
  258. var dis = new DataInputStream(stdout_pipe);
  259. string line;
  260. // Read lines from STDOUT until the process ends
  261. while ((line = dis.read_line(null)) != null) {
  262. // Look for Ninja progress pattern: "[x/x] "
  263. if (line.has_prefix("[")) {
  264. var end_bracket = line.index_of("]");
  265. if (end_bracket > 1) {
  266. var progress_str = line.substring(1, end_bracket - 1);
  267. var parts = progress_str.split("/");
  268. if (parts.length == 2) {
  269. int current_task = 0;
  270. int total_tasks = 0;
  271. // Parse the current and total task numbers
  272. if (int.try_parse(parts[0], out current_task) &&
  273. int.try_parse(parts[1], out total_tasks) &&
  274. total_tasks > 0) {
  275. // Calculate progress as a float between 0.0 and 1.0
  276. float progress = (float)current_task / (float)total_tasks;
  277. // Ensure progress is within valid bounds
  278. progress = float.max(0.0f, float.min(1.0f, progress));
  279. // Call the progress delegate with the calculated progress
  280. progress_delegate(progress);
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. } catch (Error e) {
  288. // Log any errors during progress monitoring but don't fail the build
  289. warning(@"Error monitoring build progress: $(e.message)");
  290. }
  291. };
  292. try {
  293. // Start the progress monitoring thread
  294. new Thread<void>(null, progress_thread_func);
  295. } catch (Error e) {
  296. // If we can't start the thread, log a warning but continue with the build
  297. warning(@"Failed to start progress monitoring thread: $(e.message)");
  298. }
  299. // Restore original working directory after subprocess completes
  300. Environment.set_current_dir(original_working_dir);
  301. return proc;
  302. } else {
  303. var proc = new Subprocess.newv(new string[] { path, Paths.ensure_trailing_slash(effective_build_path) }, verbose_flags(flags));
  304. // Restore original working directory after subprocess completes
  305. Environment.set_current_dir(original_working_dir);
  306. return proc;
  307. }
  308. }
  309. public Subprocess? run_rebuild(string build_path, SubprocessFlags flags) throws Error {
  310. if(executables.rebuild == null) {
  311. return null;
  312. }
  313. string original_working_dir = Environment.get_current_dir();
  314. string working_dir = original_working_dir;
  315. if (this.flags.contains(ManifestFlag.SIMPLE_BUILD_ENVIRONMENT)) {
  316. working_dir = build_path;
  317. }
  318. var path = Path.build_filename(working_dir, executables.rebuild);
  319. // Change to the working directory for subprocess execution
  320. Environment.set_current_dir(working_dir);
  321. var proc = new Subprocess.newv(new string[] { path, Paths.ensure_trailing_slash(build_path) }, verbose_flags(flags));
  322. // Restore original working directory after subprocess completes
  323. Environment.set_current_dir(original_working_dir);
  324. return proc;
  325. }
  326. public Subprocess? run_acquire(SubprocessFlags flags) throws Error {
  327. if(executables.acquire == null) {
  328. return null;
  329. }
  330. string working_dir = Environment.get_current_dir();
  331. // Note: acquire doesn't have a build_path parameter, so it can't use SIMPLE_BUILD_ENVIRONMENT
  332. var path = Path.build_filename(working_dir, executables.acquire);
  333. var proc = new Subprocess.newv(new string[] { path }, verbose_flags(flags));
  334. return proc;
  335. }
  336. public Subprocess? run_install(string build_path, string install_path, Paths paths, InstallType type, SubprocessFlags flags) throws Error {
  337. if(executables.install == null) {
  338. return null;
  339. }
  340. string original_working_dir = Environment.get_current_dir();
  341. string working_dir = original_working_dir;
  342. if (this.flags.contains(ManifestFlag.SIMPLE_BUILD_ENVIRONMENT)) {
  343. working_dir = build_path;
  344. }
  345. var path = Path.build_filename(working_dir, executables.install);
  346. // Override destination environment variable
  347. var new_paths = paths.clone();
  348. new_paths.destination = install_path;
  349. // Change to the working directory for subprocess execution
  350. Environment.set_current_dir(working_dir);
  351. new_paths.set_envs();
  352. var proc = new Subprocess.newv(new string[] { path, Paths.ensure_trailing_slash(build_path), Paths.ensure_trailing_slash(install_path), type.to_string() }, verbose_flags(flags));
  353. // Restore original working directory after subprocess completes
  354. Environment.set_current_dir(original_working_dir);
  355. return proc;
  356. }
  357. public Subprocess? run_post_install(string build_path, InstallType type, SubprocessFlags flags) throws Error {
  358. if(executables.post_install == null) {
  359. return null;
  360. }
  361. string original_working_dir = Environment.get_current_dir();
  362. string working_dir = original_working_dir;
  363. if (this.flags.contains(ManifestFlag.SIMPLE_BUILD_ENVIRONMENT)) {
  364. working_dir = build_path;
  365. }
  366. var path = Path.build_filename(working_dir, executables.post_install);
  367. // Change to the working directory for subprocess execution
  368. Environment.set_current_dir(working_dir);
  369. var proc = new Subprocess.newv(new string[] { path, Paths.ensure_trailing_slash(build_path), type.to_string() }, verbose_flags(flags));
  370. // Restore original working directory after subprocess completes
  371. Environment.set_current_dir(original_working_dir);
  372. return proc;
  373. }
  374. public Subprocess? run_remove(RemoveType type, SubprocessFlags flags) throws Error {
  375. if(executables.remove == null) {
  376. return null;
  377. }
  378. string working_dir = Environment.get_current_dir();
  379. // Note: remove doesn't have a build_path parameter, so it can't use SIMPLE_BUILD_ENVIRONMENT
  380. var path = Path.build_filename(working_dir, executables.remove);
  381. var proc = new Subprocess.newv(new string[] { path, type.to_string() }, verbose_flags(flags));
  382. return proc;
  383. }
  384. public Subprocess? run_test(string build_path, SubprocessFlags flags) throws Error {
  385. if(executables.test == null) {
  386. return null;
  387. }
  388. string original_working_dir = Environment.get_current_dir();
  389. string working_dir = original_working_dir;
  390. if (this.flags.contains(ManifestFlag.SIMPLE_BUILD_ENVIRONMENT)) {
  391. working_dir = build_path;
  392. }
  393. var path = Path.build_filename(working_dir, executables.test);
  394. // Change to the working directory for subprocess execution
  395. Environment.set_current_dir(working_dir);
  396. var proc = new Subprocess.newv(new string[] { path, Paths.ensure_trailing_slash(build_path) }, verbose_flags(flags));
  397. // Restore original working directory after subprocess completes
  398. Environment.set_current_dir(original_working_dir);
  399. return proc;
  400. }
  401. public delegate void ResourceProgressCallback(ResourceRef resource, uint current_resource, uint total_resources, float resource_frac);
  402. public void install_resources(string source_path, string build_path, string? install_path, Paths paths, ResourceProgressCallback callback, bool dry_run = false) throws Error {
  403. // Install each resource speficied by the manifest
  404. var resource_count = provides.count();
  405. var resources_installed = 0;
  406. // Install from shortest path to longest path, to ensure directories are created before children
  407. var install_order = provides.sort((a, b) => paths.get_suggested_path_for_resource(a.key).length - paths.get_suggested_path_for_resource(b.key).length);
  408. foreach (var resource in install_order) {
  409. callback(resource.key, resources_installed, resource_count, 0.0f);
  410. var path = paths.get_suggested_path_for_resource(resource.key);
  411. if(resource.key.resource_type == ResourceType.TAG) {
  412. // Ensure parent directories are created first
  413. var parent_dir = File.new_for_path(Path.get_basename(path));
  414. if(!parent_dir.query_exists() && !dry_run) {
  415. parent_dir.make_directory_with_parents();
  416. }
  417. }
  418. if(resource.value.file_type == Usm.ManifestFileType.REGULAR) {
  419. var base_path = "";
  420. if(is_data_package) {
  421. // Data packages ship every provide directly from the source
  422. // (or unpacked package) tree, regardless of declared path base
  423. base_path = source_path;
  424. }
  425. else {
  426. switch (resource.value.path_base) {
  427. case ManifestFilePathBase.BUILD:
  428. base_path = build_path;
  429. break;
  430. case ManifestFilePathBase.SOURCE:
  431. base_path = source_path;
  432. break;
  433. case ManifestFilePathBase.INSTALL:
  434. if(install_path == null) {
  435. throw new ManifestError.INVALID_FILE_PATH("Install path was not provided");
  436. }
  437. base_path = install_path;
  438. break;
  439. case ManifestFilePathBase.AS_EXPECTED:
  440. if(install_path == null) {
  441. throw new ManifestError.INVALID_FILE_PATH("Install path was not provided");
  442. }
  443. var install_paths = paths.clone();
  444. install_paths.destination = install_path;
  445. base_path = install_paths.get_suggested_path_for_resource(resource.key);
  446. break;
  447. default:
  448. assert_not_reached();
  449. }
  450. }
  451. var src = File.new_build_filename(base_path, resource.value.path ?? "");
  452. var dest = File.new_for_path(path);
  453. // Don't throw if an alternative format resource wasn't built
  454. var source_exists = src.query_exists();
  455. if(!source_exists) {
  456. throw new ManifestError.INVALID_FILE_PATH(@"Expected to find file listed in manifest at \"$(src.get_path())\", but no such file was found.");
  457. }
  458. // Don't copy an alternative format resource if it has the same path as the non-alternative resource
  459. if(source_exists && !dry_run) {
  460. if(dest.query_exists()) {
  461. dest.delete();
  462. }
  463. var parent = dest.get_parent();
  464. if(!parent.query_exists()) {
  465. parent.make_directory_with_parents(null);
  466. }
  467. src.copy(dest, FileCopyFlags.ALL_METADATA, null, (c, t) => callback(resource.key, resources_installed, resource_count, (float)c / (float)t));
  468. }
  469. }
  470. else if(resource.value.file_type == Usm.ManifestFileType.DIRECTORY) {
  471. var dest = File.new_for_path(path);
  472. if(!dry_run && !dest.query_exists()) {
  473. dest.make_directory();
  474. }
  475. }
  476. else if(resource.value.file_type == Usm.ManifestFileType.SYMBOLIC_LINK) {
  477. var dest = File.new_for_path(path);
  478. if(!dry_run && dest.query_exists()) {
  479. dest.delete();
  480. }
  481. if(!dry_run){
  482. dest.make_symbolic_link(resource.value.destination ?? "");
  483. }
  484. }
  485. else {
  486. throw new TransactionError.INSTALL_ERROR(@"Could not understand resource key \"$(resource.key)\"");
  487. }
  488. callback(resource.key, resources_installed, resource_count, 1.0f);
  489. resources_installed++;
  490. }
  491. }
  492. public void remove_resources(Paths paths, ResourceProgressCallback callback) throws Error {
  493. var non_directories = provides.where(r => r.value.file_type != Usm.ManifestFileType.DIRECTORY).cache();
  494. var directories = provides.where(r => r.value.file_type == Usm.ManifestFileType.DIRECTORY).cache();
  495. var total_operations = non_directories.count() + directories.count();
  496. var current_operation = 0;
  497. // Delete files and symlinks first
  498. foreach (var resource in non_directories) {
  499. callback(resource.key, current_operation, total_operations, 0.0f);
  500. var path = paths.get_suggested_path_for_resource(resource.key);
  501. var file = File.new_for_path(path);
  502. if(file.query_exists()) {
  503. file.delete();
  504. }
  505. callback(resource.key, current_operation, total_operations, 1.0f);
  506. current_operation++;
  507. }
  508. // Delete directories last
  509. foreach (var resource in directories) {
  510. callback(resource.key, current_operation, total_operations, 0.0f);
  511. var path = paths.get_suggested_path_for_resource(resource.key);
  512. try {
  513. var file = File.new_for_path(path);
  514. if(file.query_exists()) {
  515. file.delete();
  516. }
  517. }
  518. catch(IOError.NOT_EMPTY e) {
  519. warning(@"Did not remove resource \"$path\": directory is not empty\n");
  520. }
  521. callback(resource.key, current_operation, total_operations, 1.0f);
  522. current_operation++;
  523. }
  524. }
  525. private void copy_directory(File source, File destination) throws Error {
  526. // Ensure destination directory exists
  527. if (!destination.query_exists()) {
  528. destination.make_directory_with_parents();
  529. }
  530. var enumerator = source.enumerate_children(FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE, FileQueryInfoFlags.NONE);
  531. FileInfo file_info;
  532. while ((file_info = enumerator.next_file()) != null) {
  533. var source_child = source.get_child(file_info.get_name());
  534. var destination_child = destination.get_child(file_info.get_name());
  535. if (file_info.get_file_type() == FileType.DIRECTORY) {
  536. // Recursively copy subdirectories
  537. copy_directory(source_child, destination_child);
  538. } else {
  539. // Copy files
  540. source_child.copy(destination_child, FileCopyFlags.OVERWRITE | FileCopyFlags.ALL_METADATA);
  541. }
  542. }
  543. }
  544. }
  545. public enum InstallType {
  546. FRESH,
  547. UPGRADE,
  548. DOWNGRADE;
  549. public string to_string() {
  550. switch (this) {
  551. case InstallType.FRESH:
  552. return "fresh";
  553. case InstallType.UPGRADE:
  554. return "upgrade";
  555. case InstallType.DOWNGRADE:
  556. return "downgrade";
  557. default:
  558. assert_not_reached();
  559. }
  560. }
  561. public static InstallType from_string(string str) throws ManifestError {
  562. switch (str) {
  563. case "fresh":
  564. return InstallType.FRESH;
  565. case "upgrade":
  566. return InstallType.UPGRADE;
  567. case "downgrade":
  568. return InstallType.DOWNGRADE;
  569. default:
  570. throw new ManifestError.INVALID_REMOVE_TYPE(@"Unknown install type \"$str\".");
  571. }
  572. }
  573. }
  574. public enum RemoveType {
  575. FINAL,
  576. UPGRADE,
  577. DOWNGRADE;
  578. public string to_string() {
  579. switch (this) {
  580. case RemoveType.FINAL:
  581. return "final";
  582. case RemoveType.UPGRADE:
  583. return "upgrade";
  584. case RemoveType.DOWNGRADE:
  585. return "downgrade";
  586. default:
  587. assert_not_reached();
  588. }
  589. }
  590. public static RemoveType from_string(string str) throws ManifestError {
  591. switch (str) {
  592. case "final":
  593. return RemoveType.FINAL;
  594. case "upgrade":
  595. return RemoveType.UPGRADE;
  596. case "downgrade":
  597. return RemoveType.DOWNGRADE;
  598. default:
  599. throw new ManifestError.INVALID_REMOVE_TYPE(@"Unknown remove type \"$str\".");
  600. }
  601. }
  602. }
  603. public delegate void ProgressDelegate(float progress);
  604. public enum ManifestFlag {
  605. BUILD_IN_SOURCE_TREE,
  606. SET_MANIFEST_PROPERTY_ENVS,
  607. NINJA_STYLE_PROGRESS,
  608. SIMPLE_BUILD_ENVIRONMENT,
  609. DATA_PACKAGE;
  610. public string to_string() {
  611. switch (this) {
  612. case ManifestFlag.BUILD_IN_SOURCE_TREE:
  613. return "buildInSourceTree";
  614. case ManifestFlag.SET_MANIFEST_PROPERTY_ENVS:
  615. return "setManifestPropertyEnvs";
  616. case ManifestFlag.NINJA_STYLE_PROGRESS:
  617. return "ninjaStyleProgress";
  618. case ManifestFlag.SIMPLE_BUILD_ENVIRONMENT:
  619. return "simpleBuildEnvironment";
  620. case ManifestFlag.DATA_PACKAGE:
  621. return "dataPackage";
  622. default:
  623. assert_not_reached();
  624. }
  625. }
  626. public static ManifestFlag from_string(string str) throws ManifestError {
  627. switch (str) {
  628. case "buildInSourceTree":
  629. return ManifestFlag.BUILD_IN_SOURCE_TREE;
  630. case "setManifestPropertyEnvs":
  631. return ManifestFlag.SET_MANIFEST_PROPERTY_ENVS;
  632. case "ninjaStyleProgress":
  633. return ManifestFlag.NINJA_STYLE_PROGRESS;
  634. case "simpleBuildEnvironment":
  635. return ManifestFlag.SIMPLE_BUILD_ENVIRONMENT;
  636. case "dataPackage":
  637. return ManifestFlag.DATA_PACKAGE;
  638. default:
  639. throw new ManifestError.INVALID_FLAG(@"Unknown flag \"$str\".");
  640. }
  641. }
  642. }
  643. }