using Invercargill; namespace Usm { public class CachedPackage { public string state_path { get; private set; } public string package_path { get; private set; } public string package_name { get; private set; } public CachedPackage(string path) { this.state_path = path; this.package_path = Path.build_filename(path, "package.usmc"); this.package_name = Path.get_basename(path); } public Manifest get_manifest() throws Error { return new Manifest.from_package(package_path); } public OriginInformation get_origin_information() throws Error { return new OriginInformation.from_file(Path.build_filename(state_path, "origin-info")); } public bool has_build_directory() { return File.new_for_path(build_directory_path()).query_exists(); } public bool has_build_archive() { return File.new_for_path(build_archive_path()).query_exists(); } public string create_build_directory() throws Error { var path = build_directory_path(); File.new_for_path(path).make_directory(); return path; } public string get_build_directory() throws Error { var path = build_directory_path(); if(File.new_for_path(path).query_exists()) { return path; } var archive_path = build_archive_path(); if(!File.new_for_path(path).query_exists()) { throw new StateError.NO_BUILD_ARTIFACT(@"The package \"$package_name\" has no build artifact"); } var proc = new Subprocess.newv(new string[] { "tar", "-xf", archive_path, "-C", path }, SubprocessFlags.INHERIT_FDS); if(!proc.wait_check()) { throw new StateError.EXTRACTION_FAILED(@"Could not extract \"$archive_path\": tar returned non-zero exit status $(proc.get_status())"); } return path; } public string get_build_archive() throws Error { var path = build_archive_path(); if(File.new_for_path(path).query_exists()) { return path; } var dir_path = build_directory_path(); if(!File.new_for_path(dir_path).query_exists()) { throw new StateError.NO_BUILD_ARTIFACT(@"The package \"$package_name\" has no build artifact"); } var proc = new Subprocess.newv(new string[] { "tar", "-cJf", path, "--directory", dir_path, "." }, SubprocessFlags.INHERIT_FDS); if(!proc.wait_check()) { throw new StateError.ARCHIVAL_FAILED(@"Could not archive \"$dir_path\": tar returned non-zero exit status $(proc.get_status())"); } return path; } public void update_origin_information(OriginInformation info) throws Error { var properties = OriginInformation.get_mapper().map_from(info); var json = new InvercargillJson.JsonElement.from_properties(properties); json.write_to_file(Path.build_filename(state_path, "origin-info")); } private string build_directory_path() { return Path.build_filename(state_path, "build"); } private string build_archive_path() { return Path.build_filename(state_path, "build.tar.xz"); } } }