Manifest.vala 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. }
  12. public class Manifest {
  13. public string name { get; set; }
  14. public string summary { get; set; }
  15. public Version version { get; set; }
  16. public Vector<Licence> licences { get; set; }
  17. public Dictionary<ResourceRef, ManifestFile> provides { get; set; }
  18. public Dependencies dependencies { get; set; }
  19. public Executables executables { get; set; }
  20. public string? markdown_path { get; set; }
  21. public string? url { get; set; }
  22. public Vector<string>? screenshot_paths { get; set; }
  23. public string? icon_path { get; set; }
  24. public string? metainfo_path { get; set; }
  25. public Git? git { get; set; }
  26. public Properties? extra_properties { get; set; }
  27. public static PropertyMapper<Manifest> get_mapper() {
  28. return PropertyMapper.build_for<Manifest>(cfg => {
  29. cfg.map<string>("name", o => o.name, (o, v) => o.name = v);
  30. cfg.map<string>("version", o => o.version.to_string(), (o, v) => o.version = new Version.from_string(v));
  31. cfg.map<string>("summary", o => o.summary, (o, v) => o.summary = v);
  32. cfg.map_many_with<Licence>("licences", o => o.licences, (o, v) => o.licences = v.to_vector(), Licence.get_mapper());
  33. cfg.map<Properties>("provides", o => o.map_from_provides_dict(), (o, v) => o.build_provides_dict(v));
  34. cfg.map_with<Dependencies>("depends", o => o.dependencies, (o, v) => o.dependencies = v, Dependencies.get_mapper());
  35. cfg.map_with<Executables>("execs", o => o.executables, (o, v) => o.executables = v, Executables.get_mapper());
  36. cfg.map<string>("md", o => o.markdown_path, (o, v) => o.markdown_path = v, false);
  37. cfg.map<string>("url", o => o.url, (o, v) => o.url = v, false);
  38. cfg.map_many<string>("screenshots", o => o.screenshot_paths, (o, v) => o.screenshot_paths = v.to_vector(), false);
  39. cfg.map<string>("icon", o => o.icon_path, (o, v) => o.icon_path = v, false);
  40. cfg.map<string>("metainfo", o => o.metainfo_path, (o, v) => o.metainfo_path = v, false);
  41. cfg.map_with<Git>("git", o => o.git, (o, v) => o.git = v, Git.get_mapper(), false);
  42. cfg.map<Properties>("extras", o => o.extra_properties, (o, v) => o.extra_properties = v, false);
  43. cfg.set_constructor(() => new Manifest());
  44. });
  45. }
  46. private void build_provides_dict(Properties obj) throws Error {
  47. provides = new Dictionary<ResourceRef, ManifestFile>();
  48. var mapper = ManifestFile.get_mapper();
  49. foreach (var pair in obj) {
  50. if(pair.value.assignable_to<string>()) {
  51. provides[new ResourceRef(pair.key)] = new ManifestFile.from_string(pair.value.as<string>());
  52. }
  53. else {
  54. provides[new ResourceRef(pair.key)] = mapper.materialise(obj);
  55. }
  56. }
  57. }
  58. private Properties map_from_provides_dict() {
  59. var dict = new PropertiesDictionary();
  60. var mapper = ManifestFile.get_mapper();
  61. foreach (var pair in provides) {
  62. try {
  63. dict.set_native<Properties>(pair.key.to_string(), mapper.map_from(pair.value));
  64. }
  65. catch(Error e) {
  66. assert_not_reached();
  67. }
  68. }
  69. return dict;
  70. }
  71. public Subprocess run_build(string build_path, Paths paths, SubprocessFlags flags) throws Error {
  72. var path = Path.build_filename(Environment.get_current_dir(), executables.build);
  73. paths.set_envs();
  74. var proc = new Subprocess.newv(new string[] { path, build_path }, flags);
  75. return proc;
  76. }
  77. public Subprocess? run_rebuild(string build_path, SubprocessFlags flags) throws Error {
  78. if(executables.rebuild == null) {
  79. return null;
  80. }
  81. var path = Path.build_filename(Environment.get_current_dir(), executables.rebuild);
  82. var proc = new Subprocess.newv(new string[] { path, build_path }, flags);
  83. return proc;
  84. }
  85. public Subprocess? run_acquire(SubprocessFlags flags) throws Error {
  86. if(executables.acquire == null) {
  87. return null;
  88. }
  89. var path = Path.build_filename(Environment.get_current_dir(), executables.acquire);
  90. var proc = new Subprocess.newv(new string[] { path }, flags);
  91. return proc;
  92. }
  93. public Subprocess? run_install(string build_path, InstallType type, SubprocessFlags flags) throws Error {
  94. if(executables.install == null) {
  95. return null;
  96. }
  97. var path = Path.build_filename(Environment.get_current_dir(), executables.install);
  98. var proc = new Subprocess.newv(new string[] { path, build_path, type.to_string() }, flags);
  99. return proc;
  100. }
  101. public Subprocess? run_remove(RemoveType type, SubprocessFlags flags) throws Error {
  102. if(executables.remove == null) {
  103. return null;
  104. }
  105. var path = Path.build_filename(Environment.get_current_dir(), executables.remove);
  106. var proc = new Subprocess.newv(new string[] { path, type.to_string() }, flags);
  107. return proc;
  108. }
  109. }
  110. public enum InstallType {
  111. FRESH,
  112. UPGRADE,
  113. DOWNGRADE;
  114. public string to_string() {
  115. switch (this) {
  116. case InstallType.FRESH:
  117. return "fresh";
  118. case InstallType.UPGRADE:
  119. return "upgrade";
  120. case InstallType.DOWNGRADE:
  121. return "downgrade";
  122. default:
  123. assert_not_reached();
  124. }
  125. }
  126. public static InstallType from_string(string str) throws ManifestError {
  127. switch (str) {
  128. case "fresh":
  129. return InstallType.FRESH;
  130. case "upgrade":
  131. return InstallType.UPGRADE;
  132. case "downgrade":
  133. return InstallType.DOWNGRADE;
  134. default:
  135. throw new ManifestError.INVALID_REMOVE_TYPE(@"Unknown install type \"$str\".");
  136. }
  137. }
  138. }
  139. public enum RemoveType {
  140. FINAL,
  141. UPGRADE,
  142. DOWNGRADE;
  143. public string to_string() {
  144. switch (this) {
  145. case RemoveType.FINAL:
  146. return "final";
  147. case RemoveType.UPGRADE:
  148. return "upgrade";
  149. case RemoveType.DOWNGRADE:
  150. return "downgrade";
  151. default:
  152. assert_not_reached();
  153. }
  154. }
  155. public static RemoveType from_string(string str) throws ManifestError {
  156. switch (str) {
  157. case "final":
  158. return RemoveType.FINAL;
  159. case "upgrade":
  160. return RemoveType.UPGRADE;
  161. case "downgrade":
  162. return RemoveType.DOWNGRADE;
  163. default:
  164. throw new ManifestError.INVALID_REMOVE_TYPE(@"Unknown remove type \"$str\".");
  165. }
  166. }
  167. }
  168. }