static void UpdateCodebit(string path) { var options = new Yaml.YamlReaderOptions(); options.IgnoreTextOutsideDocumentMarkers = true; Console.WriteLine(); Console.WriteLine(path); var dict = new Dictionary <string, string>(); try { Yaml.MicroYaml.LoadFile(path, options, dict); } catch (Exception err) { Console.WriteLine(" YAML Syntax Error: " + err.Message); return; } WriteIfPresent(dict, "name"); WriteIfPresent(dict, "description"); if (!HasKeyword(dict, "CodeBit")) { Console.WriteLine(" Not a CodeBit."); return; } string localVersion; if (!dict.TryGetValue("version", out localVersion)) { Console.WriteLine(" Error: CodeBit missing version property."); return; } Console.WriteLine(" version: {0}", localVersion); string url; if (!dict.TryGetValue("url", out url)) { Console.WriteLine(" Error: CodeBit missing url property."); return; } Console.WriteLine(" url: {0}", url); Console.WriteLine(" Retrieving master copy..."); string tempFileName = null; try { if (!HttpGetToTempFile(url, Path.GetDirectoryName(path), out tempFileName)) { return; } var remoteDict = new Dictionary <string, string>(); try { Yaml.MicroYaml.LoadFile(tempFileName, options, remoteDict); } catch (Exception err) { Console.WriteLine(" YAML Syntax Error on master copy: " + err.Message); return; } if (!HasKeyword(remoteDict, "CodeBit")) { Console.WriteLine(" Master Copy is not a CodeBit."); return; } if (!remoteDict.ContainsKey("url")) { Console.WriteLine(" Master Copy is missing 'url' property."); return; } string remoteVersion; if (!remoteDict.TryGetValue("version", out remoteVersion)) { Console.WriteLine(" Error: Master CodeBit missing 'version' property."); return; } int vercomp = CompareMixedNumeric(remoteVersion, localVersion); if (vercomp == 0) { Console.WriteLine(" Local CodeBit is up to date!"); return; } if (vercomp < 0) { Console.WriteLine(" Unexpected: Local CodeBit has newer version than master copy."); Console.WriteLine(" Local version: '{0}' Master version: '{1}'", localVersion, remoteVersion); return; } Console.Write(" Master version is newer. Update local copy to version '{0}' (Y/N)? ", remoteVersion); char c = Console.ReadKey().KeyChar; if (c == 'y' || c == 'Y') { Console.WriteLine(" (Yes)"); File.Delete(path); File.Move(tempFileName, path); tempFileName = null; Console.WriteLine(" '{0}' updated to version '{1}'.", Path.GetFileName(path), remoteVersion); } else { Console.WriteLine(" (No)"); } } finally { if (!string.IsNullOrEmpty(tempFileName) && File.Exists(tempFileName)) { File.Delete(tempFileName); } } }
static void RetrieveCodeBit(string url) { var options = new Yaml.YamlReaderOptions(); options.IgnoreTextOutsideDocumentMarkers = true; Console.WriteLine("Retrieving CodeBit: " + url); Console.WriteLine(" Reading master copy from the web..."); string tempFileName = null; try { string currentDirectory = Environment.CurrentDirectory; if (!HttpGetToTempFile(url, currentDirectory, out tempFileName)) { return; } var remoteDict = new Dictionary <string, string>(); try { Yaml.MicroYaml.LoadFile(tempFileName, options, remoteDict); } catch (Exception err) { Console.WriteLine(" YAML Syntax Error on master copy: " + err.Message); return; } if (!HasKeyword(remoteDict, "CodeBit")) { Console.WriteLine(" Master Copy is not a CodeBit."); return; } if (!remoteDict.ContainsKey("url")) { Console.WriteLine(" Master Copy is missing 'url' property."); return; } string name; if (!remoteDict.TryGetValue("name", out name)) { Console.WriteLine(" Error: Master CodeBit missing 'name' property."); return; } Console.WriteLine(" name: {0}", name); WriteIfPresent(remoteDict, "description"); WriteIfPresent(remoteDict, "version"); // Create full path for the retrieved compy string targetPath = Path.Combine(currentDirectory, name); if (File.Exists(targetPath)) { Console.Write(" Local file, '{0}' already exists. Use update mode instead.", targetPath); return; } File.Move(tempFileName, targetPath); Console.WriteLine(" CodeBit '{0}' has been retrieved to '{1}'.", Path.GetFileName(targetPath), Path.GetDirectoryName(targetPath)); } finally { if (!string.IsNullOrEmpty(tempFileName) && File.Exists(tempFileName)) { File.Delete(tempFileName); } } }