Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
basic idea for updating nuget packages
using System.IO;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Diagnostics;
//version could probably come from REPL input or something
string version = "1.9.1";
/* bunch of file and folder moving around */
var packages = new []{
/* information about where the stuff is now*/
};
foreach(var package in packages) {
foreach(var source in package.sources)
{
var attr = File.GetAttributes(source);
if((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
DirectoryCopy(source, finalPath + package.destination);
}
else
{
var fileInfo = new FileInfo(source);
Console.WriteLine("Copying to " + finalPath + package.destination +"\\"+ fileInfo.Name);
fileInfo.CopyTo(finalPath + package.destination + "\\" + fileInfo.Name, true);
}
}
var packageId = UpdateNuspect(package.nuspecSource, package.nuspecDestination, dojoVersion);
PackNuSpecs(package.nuspecDestination);
//race condition with process for packing not being done when it wants to push
PushNugetPackage(packageId, version, "nuget api key");
}
public static void PushNugetPackage(string id, string version, string apikey)
{
var nugetPath = "NuGet.exe";
var workingDirectory = new FileInfo(nugetPath).DirectoryName;
var startInfo = new ProcessStartInfo(nugetPath) {
UseShellExecute = false,
Arguments = string.Format("push \"{0}\\{1}.{2}.nupkg\" -ApiKey {3}", workingDirectory, id, version, apikey),
WorkingDirectory = workingDirectory
};
var process = Process.Start(startInfo);
}
public static void PackNuSpecs(string source)
{
var nugetPath = "NuGet.exe";
var startInfo = new ProcessStartInfo(nugetPath) {
UseShellExecute = false,
Arguments = "pack \"" + source + "\"",
WorkingDirectory = new FileInfo(nugetPath).DirectoryName
};
var process = Process.Start(startInfo);
}
public static string UpdateNuspect(string source, string destination, string newVersion)
{
Console.WriteLine("Opening " + source);
//have to do local-name weird ness because of namespacing
var doc = XElement.Load(source);
var version = doc.XPathSelectElement("//*[local-name()='version']");
version.Value = newVersion;
var dependencies = doc.XPathSelectElements("//*[local-name()='dependency']");
foreach(var dependency in dependencies)
{
dependency.SetAttributeValue("version", newVersion);
}
doc.Save(destination);
return doc.XPathSelectElement("//*[local-name()='id']").Value;
}
public static void DirectoryCopy(string source, string destination)
{
// Get the subdirectories for the specified directory.
var dir = new DirectoryInfo(source);
var dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ source);
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destination))
{
Directory.CreateDirectory(destination);
}
// Get the files in the directory and copy them to the new location.
var files = dir.GetFiles();
foreach (var file in files)
{
var tempPath = Path.Combine(destination, file.Name);
file.CopyTo(tempPath, true);
}
foreach (var subdir in dirs)
{
var tempPath = Path.Combine(destination, subdir.Name);
DirectoryCopy(subdir.FullName, tempPath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment