From 7a8d164b25f3e9ba93deadbb4ab090cf4a7ee8cd Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 28 Jan 2007 04:19:46 +0000 Subject: [PATCH] Added compression support --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40409 --- tools/builder/ABuilder.cs | 181 +++++++++++++++++++++++----------- tools/builder/Config.cs | 10 +- tools/builder/LinuxBuilder.cs | 32 ++++++ tools/builder/Package.cs | 14 ++- tools/builder/PkgCore.cs | 14 ++- tools/builder/Win32Builder.cs | 31 ++++++ tools/builder/build-linux.cfg | 1 + tools/builder/build-win32.cfg | 1 + 8 files changed, 219 insertions(+), 65 deletions(-) diff --git a/tools/builder/ABuilder.cs b/tools/builder/ABuilder.cs index 5400f846..4259da00 100644 --- a/tools/builder/ABuilder.cs +++ b/tools/builder/ABuilder.cs @@ -12,8 +12,48 @@ namespace builder { } + public abstract string CompressPackage(Package pkg); + public abstract bool BuildLibrary(Package pkg, Library lib, ref string _binName, ref string _binPath); + public string GetRevsionOfPath(string path) + { + ProcessStartInfo info = new ProcessStartInfo(); + + info.WorkingDirectory = path; + info.FileName = cfg.SVNVersion; + info.Arguments = "--committed " + path; + info.UseShellExecute = false; + info.RedirectStandardOutput = true; + + Process p = Process.Start(info); + string output = p.StandardOutput.ReadToEnd(); + p.WaitForExit(); + p.Close(); + + string [] revs = output.Split(":".ToCharArray(), 2); + if (revs.Length < 1) + { + return null; + } + + string rev = null; + if (revs.Length == 1) + { + rev = revs[0]; + } + else + { + rev = revs[1]; + } + + rev = rev.Trim(); + rev = rev.Replace("M", ""); + rev = rev.Replace("S", ""); + + return rev; + } + public void UpdateRevisionInfo(Package pkg, Library lib) { string path = Config.PathFormat("{0}/{1}", cfg.SourceBase, lib.LocalPath); @@ -25,13 +65,63 @@ namespace builder } } + public void UpdateRevisionInfo(string path, string file) + { + string vers = cfg.ProductVersion.Replace(".", ","); + string rev = GetRevsionOfPath(path); + + File.Delete(file); + StreamWriter sw = File.CreateText(file); + + sw.WriteLine("/** This file is autogenerated by build scripts */"); + sw.WriteLine(""); + sw.WriteLine("#ifndef _INCLUDE_SVN_VERSION_H_"); + sw.WriteLine("#define _INCLUDE_SVN_VERSION_H_"); + sw.WriteLine(""); + sw.WriteLine("#define SVN_REVISION {0}", rev); + sw.WriteLine("#define SVN_REVISION_STRING \"{0}\"", rev); + sw.WriteLine("#define SVN_FILE_VERSION {0},{1}", vers, rev); + sw.WriteLine(""); + sw.WriteLine("#endif //_INCLUDE_SVN_VERSION_H_"); + sw.WriteLine(""); + + sw.Close(); + } + + public bool CopyFile(Package pkg, string source, string dest) + { + string from = Config.PathFormat("{0}/{1}", + cfg.SourceBase, + source); + string to = Config.PathFormat("{0}/{1}/{2}", + cfg.OutputBase, + pkg.GetBaseFolder(), + dest); + + File.Copy(from, to, true); + + return true; + } + + /** dest can be null to mean root base folder */ public void CopyFolder(Package pkg, string source, string dest, string [] omits) { string from_base = Config.PathFormat("{0}/{1}", cfg.SourceBase, source); - string to_base = Config.PathFormat("{0}/{1}/{2}", - cfg.OutputBase, - pkg.GetBaseFolder(), - dest); + string to_base = null; + + if (dest == null) + { + to_base = Config.PathFormat("{0}/{1}", + cfg.OutputBase, + pkg.GetBaseFolder()); + } + else + { + to_base = Config.PathFormat("{0}/{1}/{2}", + cfg.OutputBase, + pkg.GetBaseFolder(), + dest); + } string [] files = Directory.GetFiles(from_base); string file; @@ -61,59 +151,15 @@ namespace builder } } - public void UpdateRevisionInfo(string path, string file) + public string PackageBuildName(Package pkg) { - ProcessStartInfo info = new ProcessStartInfo(); - - info.WorkingDirectory = path; - info.FileName = cfg.SVNVersion; - info.Arguments = "--committed " + path; - info.UseShellExecute = false; - info.RedirectStandardOutput = true; - - Process p = Process.Start(info); - string output = p.StandardOutput.ReadToEnd(); - p.WaitForExit(); - p.Close(); - - string [] revs = output.Split(":".ToCharArray(), 2); - if (revs.Length < 1) - { - return; - } - - string rev = null; - if (revs.Length == 1) - { - rev = revs[0]; - } - else - { - rev = revs[1]; - } - - rev = rev.Trim(); - rev = rev.Replace("M", ""); - rev = rev.Replace("S", ""); - - string vers = cfg.ProductVersion.Replace(".", ","); - - File.Delete(file); - StreamWriter sw = File.CreateText(file); - - sw.WriteLine("/** This file is autogenerated by build scripts */"); - sw.WriteLine(""); - sw.WriteLine("#ifndef _INCLUDE_SVN_VERSION_H_"); - sw.WriteLine("#define _INCLUDE_SVN_VERSION_H_"); - sw.WriteLine(""); - sw.WriteLine("#define SVN_REVISION {0}", rev); - sw.WriteLine("#define SVN_REVISION_STRING \"{0}\"", rev); - sw.WriteLine("#define SVN_FILE_VERSION {0},{1}", vers, rev); - sw.WriteLine(""); - sw.WriteLine("#endif //_INCLUDE_SVN_VERSION_H_"); - sw.WriteLine(""); - - sw.Close(); + return pkg.GetPackageName() + + "-" + + DateTime.Now.Day.ToString("00") + + DateTime.Now.Month.ToString("00") + + DateTime.Now.Year + + "-r" + + GetRevsionOfPath(cfg.SourceBase); } public void BuildPackage(Package pkg) @@ -138,7 +184,7 @@ namespace builder /* Do primitive copies */ pkg.OnCopyFolders(this); - pkg.OnCopyFiles(); + pkg.OnCopyFiles(this); /* Do libraries */ Library [] libs = pkg.GetLibraries(); @@ -160,6 +206,27 @@ namespace builder throw new System.Exception("Failed to compile library: " + libs[i].Name); } } + + string pkg_file = null; + if ((pkg_file=CompressPackage(pkg)) == null) + { + throw new System.Exception("Failed to compress package: " + pkg.GetPackageName()); + } + + string lpath = null, ltarget = null; + pkg.GetCompressBases(ref lpath, ref ltarget); + lpath = Config.PathFormat("{0}/{1}/{2}", + cfg.OutputBase, + lpath, + pkg_file); + ltarget = Config.PathFormat("{0}/{1}", cfg.OutputBase, pkg_file); + + if (File.Exists(ltarget)) + { + File.Delete(ltarget); + } + + File.Move(lpath, ltarget); } } } diff --git a/tools/builder/Config.cs b/tools/builder/Config.cs index bb52ad95..4f7700e7 100644 --- a/tools/builder/Config.cs +++ b/tools/builder/Config.cs @@ -15,10 +15,10 @@ namespace builder public string SourceBase; public string OutputBase; public string BuilderPath; - public string CompressPath; public string CompressOptions; public string SVNVersion; public string ProductVersion; + public string Compressor; public builder.BasePlatform Platform; public Config() @@ -84,10 +84,6 @@ namespace builder { BuilderPath = val; } - else if (key.CompareTo("CompressPath") == 0) - { - CompressPath = val; - } else if (key.CompareTo("CompressOptions") == 0) { CompressOptions = val; @@ -99,6 +95,10 @@ namespace builder else if (key.CompareTo("ProductVersion") == 0) { ProductVersion = val; + } + else if (key.CompareTo("Compressor") == 0) + { + Compressor = val; } } } diff --git a/tools/builder/LinuxBuilder.cs b/tools/builder/LinuxBuilder.cs index 35ee9063..ccda2027 100644 --- a/tools/builder/LinuxBuilder.cs +++ b/tools/builder/LinuxBuilder.cs @@ -11,6 +11,38 @@ namespace builder cfg = _cfg; } + public override string CompressPackage(Package pkg) + { + string lpath = null, ltarget = null; + + pkg.GetCompressBases(ref lpath, ref ltarget); + + string local_dir = Config.PathFormat("{0}/{1}", + cfg.OutputBase, + lpath); + + string name = PackageBuildName(pkg) + ".tar.gz"; + + ProcessStartInfo info = new ProcessStartInfo(); + info.FileName = cfg.Compressor; + info.WorkingDirectory = local_dir; + info.Arguments = "zcvf \"" + name + "\" \"" + ltarget + "\""; + info.UseShellExecute = false; + + Process p = Process.Start(info); + p.WaitForExit(); + + local_dir = Config.PathFormat("{0}/{1}", local_dir, name); + + if (!File.Exists(local_dir)) + { + return null; + } + + return name; + } + + public override bool BuildLibrary(Package pkg, Library lib, ref string _binName, ref string _binPath) { ProcessStartInfo info = new ProcessStartInfo(); diff --git a/tools/builder/Package.cs b/tools/builder/Package.cs index a9965d52..481f2f6c 100644 --- a/tools/builder/Package.cs +++ b/tools/builder/Package.cs @@ -24,7 +24,12 @@ namespace builder public abstract class Package { /** - * Must return the base package folder. + * Must return the root compression point. + */ + public abstract void GetCompressBases(ref string path, ref string folder); + + /** + * Must return the base package output folder. */ public abstract string GetBaseFolder(); @@ -36,7 +41,7 @@ namespace builder /** * Called when file to file copies must be performed */ - public abstract void OnCopyFiles(); + public abstract void OnCopyFiles(ABuilder builder); /** * Called when dir to dir copies must be performed @@ -47,5 +52,10 @@ namespace builder * Called to build libraries */ public abstract Library [] GetLibraries(); + + /** + * Called to get package name + */ + public abstract string GetPackageName(); } } diff --git a/tools/builder/PkgCore.cs b/tools/builder/PkgCore.cs index 1976c1b1..0c54e565 100644 --- a/tools/builder/PkgCore.cs +++ b/tools/builder/PkgCore.cs @@ -13,6 +13,17 @@ namespace builder return "base/addons/sourcemod"; } + public override void GetCompressBases(ref string path, ref string folder) + { + path = "base"; + folder = "addons"; + } + + public override string GetPackageName() + { + return "sourcemod-core"; + } + /** * Must return the list of folders to create. */ @@ -34,7 +45,7 @@ namespace builder /** * Called when file to file copies must be performed */ - public override void OnCopyFiles() + public override void OnCopyFiles(ABuilder builder) { } @@ -51,6 +62,7 @@ namespace builder builder.CopyFolder(this, "plugins", "scripting", plugin_omits); builder.CopyFolder(this, "plugins/include", "scripting/include", null); builder.CopyFolder(this, "translations", "translations", null); + builder.CopyFolder(this, "public/licenses", null, null); } /** diff --git a/tools/builder/Win32Builder.cs b/tools/builder/Win32Builder.cs index efa0e9cf..fda67bb2 100644 --- a/tools/builder/Win32Builder.cs +++ b/tools/builder/Win32Builder.cs @@ -11,6 +11,37 @@ namespace builder cfg = _cfg; } + public override string CompressPackage(Package pkg) + { + string lpath = null, ltarget = null; + + pkg.GetCompressBases(ref lpath, ref ltarget); + + string local_dir = Config.PathFormat("{0}/{1}", + cfg.OutputBase, + lpath); + + string name = PackageBuildName(pkg) + ".zip"; + + ProcessStartInfo info = new ProcessStartInfo(); + info.FileName = cfg.Compressor; + info.WorkingDirectory = local_dir; + info.Arguments = "-r \"" + name + "\" \"" + ltarget + "\""; + info.UseShellExecute = false; + + Process p = Process.Start(info); + p.WaitForExit(); + + local_dir = Config.PathFormat("{0}/{1}", local_dir, name); + + if (!File.Exists(local_dir)) + { + return null; + } + + return name; + } + public override bool BuildLibrary(Package pkg, Library lib, ref string _binName, ref string _binPath) { ProcessStartInfo info = new ProcessStartInfo(); diff --git a/tools/builder/build-linux.cfg b/tools/builder/build-linux.cfg index 2ac4b5b6..918abd32 100644 --- a/tools/builder/build-linux.cfg +++ b/tools/builder/build-linux.cfg @@ -3,3 +3,4 @@ SourceBase = /home/users/dvander/sourcemod/trunk BuilderPath = /usr/bin/make SVNVersion = /usr/bin/svnversion ProductVersion = 1.0.0 +Compressor = /bin/tar \ No newline at end of file diff --git a/tools/builder/build-win32.cfg b/tools/builder/build-win32.cfg index 0a4a86a9..586d44e2 100644 --- a/tools/builder/build-win32.cfg +++ b/tools/builder/build-win32.cfg @@ -3,3 +3,4 @@ SourceBase = r:\sourcemod\trunk BuilderPath = C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.com SVNVersion = C:\Program Files\Subversion\bin\svnversion.exe ProductVersion = 1.0.0 +Compressor = c:\Windows\zip.exe