Manifest.vala 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. using Invercargill;
  2. namespace Usm {
  3. public errordomain ManifestError {
  4. MISSING_FIELD,
  5. INVALID_VERSION,
  6. INVALID_LICENCE_CATEGORY,
  7. INVALID_RESOURCE_TYPE,
  8. INVALID_FILE_TYPE,
  9. INVALID_REMOVE_TYPE,
  10. INVALID_INSTALL_TYPE,
  11. INVALID_PACKAGE,
  12. INVALID_PATH_BASE,
  13. INVALID_FILE_PATH,
  14. INVALID_FLAG
  15. }
  16. public class Manifest {
  17. public string name { get; set; }
  18. public string summary { get; set; }
  19. public Version version { get; set; }
  20. public Vector<Licence> licences { get; set; }
  21. public Dictionary<ResourceRef, ManifestFile> provides { get; set; }
  22. public Dependencies dependencies { get; set; }
  23. public Executables executables { get; set; }
  24. public Set<ManifestFlag> flags { get; set; }
  25. public string? markdown_path { get; set; }
  26. public string? url { get; set; }
  27. public Vector<string>? screenshot_paths { get; set; }
  28. public string? icon_path { get; set; }
  29. public string? metainfo_path { get; set; }
  30. public Git? git { get; set; }
  31. public Properties? extra_properties { get; set; }
  32. public static PropertyMapper<Manifest> get_mapper() {
  33. return PropertyMapper.build_for<Manifest>(cfg => {
  34. cfg.map<string>("name", o => o.name, (o, v) => o.name = v);
  35. cfg.map<string>("version", o => o.version.to_string(), (o, v) => o.version = new Version.from_string(v));
  36. cfg.map<string>("summary", o => o.summary, (o, v) => o.summary = v);
  37. cfg.map_property_groups_with<Licence>("licences", o => o.licences, (o, v) => o.licences = v.to_vector(), Licence.get_mapper());
  38. cfg.map<Properties>("provides", o => o.map_from_provides_dict(), (o, v) => o.build_provides_dict(v));
  39. cfg.map_properties_with<Dependencies>("depends", o => o.dependencies, (o, v) => o.dependencies = v, Dependencies.get_mapper());
  40. cfg.map_properties_with<Executables>("execs", o => o.executables, (o, v) => o.executables = v, Executables.get_mapper());
  41. 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());
  42. cfg.map<string>("md", o => o.markdown_path, (o, v) => o.markdown_path = v)
  43. .undefined_when(o => o.markdown_path == null)
  44. .when_undefined(o => o.markdown_path = null);
  45. cfg.map<string>("url", o => o.url, (o, v) => o.url = v)
  46. .undefined_when(o => o.url == null)
  47. .when_undefined(o => o.url = null);
  48. cfg.map_many<string>("screenshots", o => o.screenshot_paths, (o, v) => o.screenshot_paths = v.to_vector())
  49. .undefined_when(o => o.screenshot_paths == null)
  50. .when_undefined(o => o.screenshot_paths = null);
  51. cfg.map<string>("icon", o => o.icon_path, (o, v) => o.icon_path = v)
  52. .undefined_when(o => o.icon_path == null)
  53. .when_undefined(o => o.icon_path = null);
  54. cfg.map<string>("metainfo", o => o.metainfo_path, (o, v) => o.metainfo_path = v)
  55. .undefined_when(o => o.metainfo_path == null)
  56. .when_undefined(o => o.metainfo_path = null);
  57. // cfg.map_with<Git>("git", o => o.git, (o, v) => o.git = v, Git.get_mapper(), false);
  58. cfg.map<Properties>("extras", o => o.extra_properties, (o, v) => o.extra_properties = v)
  59. .undefined_when(o => o.extra_properties == null)
  60. .when_undefined(o => o.extra_properties = null);
  61. cfg.set_constructor(() => new Manifest());
  62. });
  63. }
  64. public Manifest.from_file(string path) throws Error {
  65. var element = new InvercargillJson.JsonElement.from_file(path);
  66. Manifest.get_mapper().map_into(this, element.as<Invercargill.Properties>());
  67. }
  68. public Manifest.from_package(string path) throws Error {
  69. var archive = new Archive.Read();
  70. archive.support_format_tar();
  71. archive.support_filter_xz();
  72. var result = archive.open_filename(path, 10240);
  73. if(result != Archive.Result.OK) {
  74. throw new ManifestError.INVALID_PACKAGE("Could not read archive");
  75. }
  76. unowned Archive.Entry entry;
  77. while(archive.next_header(out entry) == Archive.Result.OK) {
  78. var path_name = entry.pathname();
  79. if(path_name != "./MANIFEST.usm") {
  80. continue;
  81. }
  82. var manifest_blob = new BinaryData();
  83. uint8[] buffer;
  84. Posix.off_t offset;
  85. while (archive.read_data_block (out buffer, out offset) == Archive.Result.OK) {
  86. manifest_blob.append_byte_array(buffer[offset:]);
  87. }
  88. var element = new InvercargillJson.JsonElement.from_string(manifest_blob.to_raw_string());
  89. Manifest.get_mapper().map_into(this, element.as<Invercargill.Properties>());
  90. return;
  91. }
  92. throw new ManifestError.INVALID_PACKAGE("MANIFEST.usm not found within archive");
  93. }
  94. private void build_provides_dict(Properties obj) throws Error {
  95. provides = new Dictionary<ResourceRef, ManifestFile>();
  96. var mapper = ManifestFile.get_mapper();
  97. foreach (var pair in obj) {
  98. if(pair.value.assignable_to<string>()) {
  99. provides[new ResourceRef(pair.key)] = new ManifestFile.from_string(pair.value.as<string>());
  100. }
  101. else {
  102. provides[new ResourceRef(pair.key)] = mapper.materialise(pair.value.as<Properties>());
  103. }
  104. }
  105. }
  106. private Properties map_from_provides_dict() {
  107. var dict = new PropertiesDictionary();
  108. var mapper = ManifestFile.get_mapper();
  109. foreach (var pair in provides) {
  110. try {
  111. dict.set_native<Properties>(pair.key.to_string(), mapper.map_from(pair.value));
  112. }
  113. catch(Error e) {
  114. assert_not_reached();
  115. }
  116. }
  117. return dict;
  118. }
  119. public Subprocess run_build(string build_path, Paths paths, SubprocessFlags flags) throws Error {
  120. var path = Path.build_filename(Environment.get_current_dir(), executables.build);
  121. paths.set_envs();
  122. var proc = new Subprocess.newv(new string[] { path, build_path }, flags);
  123. return proc;
  124. }
  125. public Subprocess? run_rebuild(string build_path, SubprocessFlags flags) throws Error {
  126. if(executables.rebuild == null) {
  127. return null;
  128. }
  129. var path = Path.build_filename(Environment.get_current_dir(), executables.rebuild);
  130. var proc = new Subprocess.newv(new string[] { path, build_path }, flags);
  131. return proc;
  132. }
  133. public Subprocess? run_acquire(SubprocessFlags flags) throws Error {
  134. if(executables.acquire == null) {
  135. return null;
  136. }
  137. var path = Path.build_filename(Environment.get_current_dir(), executables.acquire);
  138. var proc = new Subprocess.newv(new string[] { path }, flags);
  139. return proc;
  140. }
  141. public Subprocess? run_install(string build_path, string install_path, Paths paths, InstallType type, SubprocessFlags flags) throws Error {
  142. if(executables.install == null) {
  143. return null;
  144. }
  145. var path = Path.build_filename(Environment.get_current_dir(), executables.install);
  146. // Override destination environment variable
  147. var new_paths = paths.clone();
  148. new_paths.destination = install_path;
  149. new_paths.set_envs();
  150. var proc = new Subprocess.newv(new string[] { path, build_path, install_path, type.to_string() }, flags);
  151. return proc;
  152. }
  153. public Subprocess? run_post_install(string build_path, InstallType type, SubprocessFlags flags) throws Error {
  154. if(executables.post_install == null) {
  155. return null;
  156. }
  157. var path = Path.build_filename(Environment.get_current_dir(), executables.post_install);
  158. var proc = new Subprocess.newv(new string[] { path, build_path, type.to_string() }, flags);
  159. return proc;
  160. }
  161. public Subprocess? run_remove(RemoveType type, SubprocessFlags flags) throws Error {
  162. if(executables.remove == null) {
  163. return null;
  164. }
  165. var path = Path.build_filename(Environment.get_current_dir(), executables.remove);
  166. var proc = new Subprocess.newv(new string[] { path, type.to_string() }, flags);
  167. return proc;
  168. }
  169. public delegate void ResourceProgressCallback(ResourceRef resource, int current_resource, int total_resources, float resource_frac);
  170. public void install_resources(string source_path, string build_path, string? install_path, Paths paths, ResourceProgressCallback callback, bool dry_run = false) throws Error {
  171. // Install each resource speficied by the manifest
  172. var resource_count = provides.count();
  173. var resources_installed = 0;
  174. // Install from shortest path to longest path, to ensure directories are created before children
  175. var install_order = provides.sort((a, b) => paths.get_suggested_path(a.key).length - paths.get_suggested_path(b.key).length);
  176. foreach (var resource in install_order) {
  177. callback(resource.key, resources_installed, resource_count, 0.0f);
  178. var path = paths.get_suggested_path(resource.key);
  179. if(resource.key.resource_type == ResourceType.TAG) {
  180. // Ensure parent directories are created first
  181. var parent_dir = File.new_for_path(Path.get_basename(path));
  182. if(!parent_dir.query_exists() && !dry_run) {
  183. parent_dir.make_directory_with_parents();
  184. }
  185. }
  186. if(resource.value.file_type == Usm.ManifestFileType.REGULAR) {
  187. var base_path = "";
  188. switch (resource.value.path_base) {
  189. case ManifestFilePathBase.BUILD:
  190. base_path = build_path;
  191. break;
  192. case ManifestFilePathBase.SOURCE:
  193. base_path = source_path;
  194. break;
  195. case ManifestFilePathBase.INSTALL:
  196. if(install_path == null) {
  197. throw new ManifestError.INVALID_FILE_PATH("Install path was not provided");
  198. }
  199. base_path = install_path;
  200. break;
  201. case ManifestFilePathBase.AS_EXPECTED:
  202. if(install_path == null) {
  203. throw new ManifestError.INVALID_FILE_PATH("Install path was not provided");
  204. }
  205. base_path = Path.build_filename(install_path, paths.get_suggested_path(resource.key));
  206. break;
  207. default:
  208. assert_not_reached();
  209. }
  210. var src = File.new_build_filename(base_path, resource.value.path);
  211. var dest = File.new_for_path(path);
  212. if(!src.query_exists()) {
  213. throw new ManifestError.INVALID_FILE_PATH(@"Expected to find file listed in manifest at \"$(src.get_path())\", but no such file was found.");
  214. }
  215. if(!dry_run) {
  216. src.copy(dest, FileCopyFlags.OVERWRITE, null, (c, t) => callback(resource.key, resources_installed, resource_count, (float)c / (float)t));
  217. }
  218. }
  219. else if(resource.value.file_type == Usm.ManifestFileType.DIRECTORY) {
  220. var dest = File.new_for_path(path);
  221. if(!dry_run) {
  222. dest.make_directory();
  223. }
  224. }
  225. else if(resource.value.file_type == Usm.ManifestFileType.SYMBOLIC_LINK) {
  226. var dest = File.new_for_path(path);
  227. if(!dry_run){
  228. dest.make_symbolic_link(resource.value.path);
  229. }
  230. }
  231. else {
  232. throw new TransactionError.INSTALL_ERROR(@"Could not understand resource key \"$(resource.key)\"");
  233. }
  234. callback(resource.key, resources_installed, resource_count, 1.0f);
  235. resources_installed++;
  236. }
  237. }
  238. public void remove_resources(Paths paths, ResourceProgressCallback callback) throws Error {
  239. var non_directories = provides.where(r => r.value.file_type != Usm.ManifestFileType.DIRECTORY).cache();
  240. var directories = provides.where(r => r.value.file_type == Usm.ManifestFileType.DIRECTORY).cache();
  241. var total_operations = non_directories.count() + directories.count();
  242. var current_operation = 0;
  243. // Delete files and symlinks first
  244. foreach (var resource in non_directories) {
  245. callback(resource.key, current_operation, total_operations, 0.0f);
  246. var path = paths.get_suggested_path(resource.key);
  247. var file = File.new_for_path(path);
  248. if(file.query_exists()) {
  249. file.delete();
  250. }
  251. callback(resource.key, current_operation, total_operations, 1.0f);
  252. current_operation++;
  253. }
  254. // Delete directories last
  255. foreach (var resource in directories) {
  256. callback(resource.key, current_operation, total_operations, 0.0f);
  257. var path = paths.get_suggested_path(resource.key);
  258. try {
  259. var file = File.new_for_path(path);
  260. if(file.query_exists()) {
  261. file.delete();
  262. }
  263. }
  264. catch(IOError.NOT_EMPTY e) {
  265. warning(@"Did not remove resource \"$path\": directory is not empty\n");
  266. }
  267. callback(resource.key, current_operation, total_operations, 1.0f);
  268. current_operation++;
  269. }
  270. }
  271. }
  272. public enum InstallType {
  273. FRESH,
  274. UPGRADE,
  275. DOWNGRADE;
  276. public string to_string() {
  277. switch (this) {
  278. case InstallType.FRESH:
  279. return "fresh";
  280. case InstallType.UPGRADE:
  281. return "upgrade";
  282. case InstallType.DOWNGRADE:
  283. return "downgrade";
  284. default:
  285. assert_not_reached();
  286. }
  287. }
  288. public static InstallType from_string(string str) throws ManifestError {
  289. switch (str) {
  290. case "fresh":
  291. return InstallType.FRESH;
  292. case "upgrade":
  293. return InstallType.UPGRADE;
  294. case "downgrade":
  295. return InstallType.DOWNGRADE;
  296. default:
  297. throw new ManifestError.INVALID_REMOVE_TYPE(@"Unknown install type \"$str\".");
  298. }
  299. }
  300. }
  301. public enum RemoveType {
  302. FINAL,
  303. UPGRADE,
  304. DOWNGRADE;
  305. public string to_string() {
  306. switch (this) {
  307. case RemoveType.FINAL:
  308. return "final";
  309. case RemoveType.UPGRADE:
  310. return "upgrade";
  311. case RemoveType.DOWNGRADE:
  312. return "downgrade";
  313. default:
  314. assert_not_reached();
  315. }
  316. }
  317. public static RemoveType from_string(string str) throws ManifestError {
  318. switch (str) {
  319. case "final":
  320. return RemoveType.FINAL;
  321. case "upgrade":
  322. return RemoveType.UPGRADE;
  323. case "downgrade":
  324. return RemoveType.DOWNGRADE;
  325. default:
  326. throw new ManifestError.INVALID_REMOVE_TYPE(@"Unknown remove type \"$str\".");
  327. }
  328. }
  329. }
  330. public enum ManifestFlag {
  331. BUILD_IN_SOURCE_TREE,
  332. SET_MANIFEST_PROPERTY_ENVS;
  333. public string to_string() {
  334. switch (this) {
  335. case ManifestFlag.BUILD_IN_SOURCE_TREE:
  336. return "buildInSourceTree";
  337. case ManifestFlag.SET_MANIFEST_PROPERTY_ENVS:
  338. return "setManifestPropertyEnvs";
  339. default:
  340. assert_not_reached();
  341. }
  342. }
  343. public static ManifestFlag from_string(string str) throws ManifestError {
  344. switch (str) {
  345. case "buildInSourceTree":
  346. return ManifestFlag.BUILD_IN_SOURCE_TREE;
  347. case "setManifestPropertyEnvs":
  348. return ManifestFlag.SET_MANIFEST_PROPERTY_ENVS;
  349. default:
  350. throw new ManifestError.INVALID_FLAG(@"Unknown flag \"$str\".");
  351. }
  352. }
  353. }
  354. }