CachedPackage.vala 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Invercargill;
  2. namespace Usm {
  3. public class CachedPackage {
  4. public string state_path { get; private set; }
  5. public string package_path { get; private set; }
  6. public string package_name { get; private set; }
  7. public CachedPackage(string path) {
  8. this.state_path = path;
  9. this.package_path = Path.build_filename(path, "package.usmc");
  10. this.package_name = Path.get_basename(path);
  11. }
  12. public Manifest get_manifest() throws Error {
  13. return new Manifest.from_package(package_path);
  14. }
  15. public OriginInformation get_origin_information() throws Error {
  16. return new OriginInformation.from_file(Path.build_filename(state_path, "origin-info"));
  17. }
  18. public bool has_build_directory() {
  19. return File.new_for_path(build_directory_path()).query_exists();
  20. }
  21. public bool has_build_archive() {
  22. return File.new_for_path(build_archive_path()).query_exists();
  23. }
  24. public string create_build_directory() throws Error {
  25. var path = build_directory_path();
  26. File.new_for_path(path).make_directory();
  27. return path;
  28. }
  29. public string get_build_directory() throws Error {
  30. var path = build_directory_path();
  31. if(File.new_for_path(path).query_exists()) {
  32. return path;
  33. }
  34. var archive_path = build_archive_path();
  35. if(!File.new_for_path(path).query_exists()) {
  36. throw new StateError.NO_BUILD_ARTIFACT(@"The package \"$package_name\" has no build artifact");
  37. }
  38. var proc = new Subprocess.newv(new string[] { "tar", "-xf", archive_path, "-C", path }, SubprocessFlags.INHERIT_FDS);
  39. if(!proc.wait_check()) {
  40. throw new StateError.EXTRACTION_FAILED(@"Could not extract \"$archive_path\": tar returned non-zero exit status $(proc.get_status())");
  41. }
  42. return path;
  43. }
  44. public string get_build_archive() throws Error {
  45. var path = build_archive_path();
  46. if(File.new_for_path(path).query_exists()) {
  47. return path;
  48. }
  49. var dir_path = build_directory_path();
  50. if(!File.new_for_path(dir_path).query_exists()) {
  51. throw new StateError.NO_BUILD_ARTIFACT(@"The package \"$package_name\" has no build artifact");
  52. }
  53. var proc = new Subprocess.newv(new string[] { "tar", "-cJf", path, "--directory", dir_path, "." }, SubprocessFlags.INHERIT_FDS);
  54. if(!proc.wait_check()) {
  55. throw new StateError.ARCHIVAL_FAILED(@"Could not archive \"$dir_path\": tar returned non-zero exit status $(proc.get_status())");
  56. }
  57. return path;
  58. }
  59. public void update_origin_information(OriginInformation info) throws Error {
  60. var properties = OriginInformation.get_mapper().map_from(info);
  61. var json = new InvercargillJson.JsonElement.from_properties(properties);
  62. json.write_to_file(Path.build_filename(state_path, "origin-info"));
  63. }
  64. private string build_directory_path() {
  65. return Path.build_filename(state_path, "build");
  66. }
  67. private string build_archive_path() {
  68. return Path.build_filename(state_path, "build.tar.xz");
  69. }
  70. }
  71. }