} // end function init public void ReadManifest() { if (!File.Exists("manifest.json")) { throw new Exception("Manifest not present. You're looking to configure a new release? Try with 'init' command."); } string json = File.ReadAllText("manifest.json"); //json = RealSimpleNet.Helpers.Crypt.Decrypt(json); currentManifest = serializer.Deserialize <models.Manifest>(json); currentManifest.ftpcredentials.Decrypt(); }
private void SaveManifest(models.Manifest manifest) { manifest.ftpcredentials.Encrypt(); string json = serializer.Serialize(manifest); json = RealSimpleNet.Helpers.Json.PrettyPrint(json); File.WriteAllText( "manifest.json", json ); // end write all text }
} // void update /// <summary> /// Validates main exists, and if so, start the main process /// </summary> /// <param name="currentManifest"></param> private void StartMain(models.Manifest currentManifest) { // Validate main exists if (!File.Exists(currentManifest.main)) { throw new Exception(string.Format("Main file '{0}' do not exists", currentManifest.main)); } else { Log("Starting main {0}", currentManifest.main); Process.Start(currentManifest.main); } // end if file exists } // end function StartMain
} // end void /// <summary> /// Validates all files in manifest exists and ok (checksum verified) /// </summary> /// <param name="manifest">The manifest to validate</param> private void ValidateManifest(models.Manifest manifest) { manifest.files.ForEach(f => { if (!File.Exists(f.filename)) { throw new FileNotFoundException(string.Format("{0} does not exists", f.filename)); } // end if file do not exists if (f.filename != "manifest.json" && f.checksum != RealSimpleNet.Helpers.Crypt.Checksum(f.filename)) { throw new Exception(string.Format("Checksum of file {0} does not match", f.filename)); } // end if check sum do not match }); // end for each file in the manifest } // end function ValidateManifest
} // end function backup monitored file /// <summary> /// Downloads a monitored file /// </summary> /// <param name="http"></param> /// <param name="version"></param> /// <param name="fileName"></param> private void DownloadMonitoredFile( ref RealSimpleNet.Helpers.Http http, string version, models.Manifest manifest, string fileName) { // Prepare folder structure Console.WriteLine("Download Monitored file: " + fileName); // The separator string[] sep = new string[] { "\\" }; // Split by directorty separator string[] parts = fileName.Split(sep, StringSplitOptions.RemoveEmptyEntries); // If there's more than one part, then the file is a subdirectory if (parts.Length > 1) { int i = 0; string treeDir = ""; // Loop through the directories for (i = 0; i < parts.Length - 1; i++) { // Setup the tree (parent and current directories) treeDir += "\\" + parts[i]; // If not exists, created if (!Directory.Exists(treeDir)) { Directory.CreateDirectory(treeDir); } // end if not exists } // end for } // end if parts > 1 // Setup source and destination string source = string.Format("{0}/{1}/{2}", manifest.ftpcredentials.Url, version, fileName); string destination = fileName + ".tmp"; // Actually download the file http.Download( source, destination, manifest.ftpcredentials.User, manifest.ftpcredentials.Pwd ); // end download } // end function DownloadManifestFile
public void Init(string appname, string main, string url, string user, string pwd) { // Creates the manifest, the json file // The directory string dir = Directory.GetCurrentDirectory() + "\\"; models.Manifest manifest = new models.Manifest(); manifest.appname = appname; manifest.version = "1.0.0.0"; manifest.main = main; manifest.ftpcredentials = new models.Credentials( url, user, pwd ); this.ParseDirectory(dir, dir, ref manifest); // Saves the manifest to file SaveManifest(manifest); } // end function init
public void ParseDirectory(string rootDir, string dir, ref models.Manifest manifest) { string[] files = Directory.GetFiles(dir); string[] directories = Directory.GetDirectories(dir); foreach (string file in files) { Log("Parsing file: {0}", file); if ( file.ToLower() == (AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName).ToLower() || file.ToLower() == (AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName.Replace(".exe", ".dll")).ToLower() ) { Console.WriteLine("Ignoring " + AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName); continue; } // end if same name manifest.AddFile( new models.MonitoredFile( file.Replace(rootDir, ""), RealSimpleNet.Helpers.Crypt.Checksum(file) ) // end new model.MonitoredFile ); // end manifest add string main = manifest.main; if (manifest.files.Find(f => f.filename == main) == null) { throw new Exception(string.Format("Main file '{0}' not present in manifest", main)); } // end if main is not in the manifest } // end foreach foreach (string directory in directories) { ParseDirectory(rootDir, directory, ref manifest); } // end for each directory } // end function ParseDirectory
} // end function ValidateManifest /// <summary> /// Upgrades the application /// First read the manifest /// Then look for the latest version /// If the latest version is bigger than the current version, updates the application /// Download new manifest /// Compare the files in both manifests /// If there's an upgrade, backup current file and download the new one /// </summary> public void Upgrade() { // Read the manifest this.ReadManifest(); // Get the current manifest string currentVersion = this.currentManifest.version; Log("Current version is {0}", currentVersion); // This will download the files RealSimpleNet.Helpers.Http http = new RealSimpleNet.Helpers.Http(); // Donwload the latest flag http.Download( currentManifest.ftpcredentials.Url + "/latest", "latest.tmp", currentManifest.ftpcredentials.User, currentManifest.ftpcredentials.Pwd ); // end http download // Read the downloaded flag string version = File.ReadAllText("latest.tmp"); Log("Last version is {0}", version); // Here we compare versions if (version.CompareTo(currentManifest.version) < 0) { // If same version, exit Log("Old version. Exiting"); return; } if (version.CompareTo(currentManifest.version) == 0) { // If same version, exit Log("Same version. Check if local files exists"); // Check files in manifest exists bool existFiles = true; currentManifest.files.ForEach(f => existFiles = existFiles && File.Exists(f.filename)); if (existFiles) { Log("Local files exists. Exiting"); StartMain(currentManifest); return; } else { Log("Local files do not exists. Upgrading."); } // end if then else existFiles } // end if same version Log("New version, updating"); // Another version // Download new manifest http.Download( currentManifest.ftpcredentials.Url + "/" + version + "/manifest.json", "manifest.json.tmp", currentManifest.ftpcredentials.User, currentManifest.ftpcredentials.Pwd ); // end http download Log("New manifest downloaded"); // Load new manifest string tmpJson = File.ReadAllText("manifest.json.tmp"); models.Manifest tmpManifest = serializer.Deserialize <models.Manifest>(tmpJson); tmpManifest.ftpcredentials.Decrypt(); Log("New manifest loaded in memory. Start comparing files."); List <models.MonitoredFile> updatedFiles = new List <models.MonitoredFile>(); // Compare files from new manifest with the oldone foreach (models.MonitoredFile file in tmpManifest.files) { Log("Comparing " + file.filename + "..."); models.MonitoredFile localFile = null; localFile = currentManifest.files.FirstOrDefault(f => f.filename == file.filename); if (localFile == null || !File.Exists(file.filename)) { Log("New file {0}. Downloading...", file.filename); // Donwload new file this.DownloadMonitoredFile( ref http, version, currentManifest, file.filename ); // end DownloadMonitoredFile // Add to the list updatedFiles.Add(file); Console.WriteLine("add updated file " + file.filename); Log("{0} downloaded.", file.filename); } else { // Compare the checksum if (!file.checksum.Equals(localFile.checksum)) { Log("New version file {0}. Updating...", file.filename); // Donwload new file this.DownloadMonitoredFile( ref http, version, currentManifest, file.filename ); // end DownloadMonitoredFile // Add to the list updatedFiles.Add(file); Console.WriteLine("add updated file " + file.filename); Log("{0} downloaded.", file.filename + ".tmp"); } // end if diff checsum } // end if then else local file is null } // end foreach file // Loop the updated files // Backup the files // Rename the files updatedFiles.ForEach(f => { Console.WriteLine("updateFile: " + f.filename); // Here exists and has different chechsum // Backup the file, move it this.BackupMonitoredFile(currentVersion, f.filename); Log("{0} backed up.", f.filename); // Delete if exists if (File.Exists(f.filename)) { File.Delete(f.filename); } // end if file exists // Rename the downloaded file is exists if (!File.Exists(f.filename + ".tmp")) { throw new Exception(string.Format("{0} file was not downloaded correctly", f.filename)); } // end if not exists downloaded file // Rename the files Console.WriteLine("Moving file " + f.filename + ".tmp"); File.Move(f.filename + ".tmp", f.filename); }); // end for each file // if the file exists in local, but not in remote, do nothing // Validates the latest manifest Log("Validating the manifest"); ValidateManifest(tmpManifest); StartMain(currentManifest); } // void update