diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-08 01:27:52 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-08 01:27:52 -0400 |
commit | ddad601de3610a56a87c3f943d7ecc9c92af15f9 (patch) | |
tree | c6c23f044d3049973e41a0f15927168e691740bb /src/SMAPI.ModBuildConfig/build | |
parent | af68910685326a660cc88cd92582b38cbc0d9b2f (diff) | |
download | SMAPI-ddad601de3610a56a87c3f943d7ecc9c92af15f9.tar.gz SMAPI-ddad601de3610a56a87c3f943d7ecc9c92af15f9.tar.bz2 SMAPI-ddad601de3610a56a87c3f943d7ecc9c92af15f9.zip |
move create-zip task into project code
Diffstat (limited to 'src/SMAPI.ModBuildConfig/build')
-rw-r--r-- | src/SMAPI.ModBuildConfig/build/smapi.targets | 120 |
1 files changed, 2 insertions, 118 deletions
diff --git a/src/SMAPI.ModBuildConfig/build/smapi.targets b/src/SMAPI.ModBuildConfig/build/smapi.targets index a1b6aab3..b4bc8d8b 100644 --- a/src/SMAPI.ModBuildConfig/build/smapi.targets +++ b/src/SMAPI.ModBuildConfig/build/smapi.targets @@ -1,124 +1,8 @@ <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <!--********************************************* - ** Define build tasks + ** Import build tasks **********************************************--> - <!--###### - ## create a release zip file for a mod (CodeTaskFactory only available on Windows?) - #######--> - <UsingTask TaskName="CreateModReleaseZip" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" Condition="'$(OS)' == 'Windows_NT'"> - <ParameterGroup> - <ModName ParameterType="System.String" Required="true" /> - <Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> - <OutputFolderPath ParameterType="System.String" Required="true" /> - </ParameterGroup> - <Task> - <Reference Include="System.IO" /> - <Reference Include="System.IO.Compression" /> - <Reference Include="System.Web.Extensions"/> - <Code Type="Class" Language="cs"> - <![CDATA[ - using System; - using System.Collections.Generic; - using System.IO; - using System.IO.Compression; - using System.Web.Script.Serialization; - using Microsoft.Build.Framework; - using Microsoft.Build.Utilities; - - /// <summary>A build task which packs mod files into a conventional release zip.</summary> - public class CreateModReleaseZip : Task, ITask - { - /********* - ** Accessors - *********/ - /// <summary>The mod files to pack.</summary> - public ITaskItem[] Files { get; set; } - - /// <summary>The name of the mod.</param> - public string ModName { get; set; } - - /// <summary>The absolute or relative path to the folder which should contain the generated zip file.</summary> - public string OutputFolderPath { get; set; } - - - /********* - ** Public methods - *********/ - public override bool Execute() - { - try - { - // create output path if needed - Directory.CreateDirectory(OutputFolderPath); - - // get zip filename - string fileName = string.Format("{0}-{1}.zip", this.ModName, this.GetManifestVersion()); - - // clear old zip file if present - string zipPath = Path.Combine(OutputFolderPath, fileName); - if (File.Exists(zipPath)) - File.Delete(zipPath); - - // create zip file - using (Stream zipStream = new FileStream(zipPath, FileMode.Create, FileAccess.Write)) - using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) - { - foreach (ITaskItem file in Files) - { - // get file info - string filePath = file.ItemSpec; - string entryName = ModName + '/' + file.GetMetadata("RecursiveDir") + file.GetMetadata("Filename") + file.GetMetadata("Extension"); - if (new FileInfo(filePath).Directory.Name.Equals("i18n", StringComparison.InvariantCultureIgnoreCase)) - entryName = Path.Combine("i18n", entryName); - - // add to zip - using (Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) - using (Stream fileStreamInZip = archive.CreateEntry(entryName).Open()) - { - fileStream.CopyTo(fileStreamInZip); - } - } - } - - return true; - } - catch (Exception ex) - { - Log.LogErrorFromException(ex); - return false; - } - } - - /// <summary>Get a semantic version from the mod manifest (if available).</summary> - public string GetManifestVersion() - { - // Get the file JSON string - string json = ""; - foreach(ITaskItem file in Files) - { - if(Path.GetFileName(file.ItemSpec).ToLower() != "manifest.json") - continue; - json = File.ReadAllText(file.ItemSpec); - break; - } - - // Serialize the manifest json into a data object, then get a version object from that. - IDictionary<string, object> data = (IDictionary<string, object>)new JavaScriptSerializer().DeserializeObject(json); - IDictionary<string, object> version = (IDictionary<string, object>)data["Version"]; - - // Store our version numbers for ease of use - int major = (int)version["MajorVersion"]; - int minor = (int)version["MinorVersion"]; - int patch = (int)version["PatchVersion"]; - - return String.Format("{0}.{1}.{2}", major, minor, patch); - } - } - ]]> - </Code> - </Task> - </UsingTask> - + <UsingTask TaskName="CreateModReleaseZip" AssemblyFile="StardewModdingAPI.ModBuildConfig.dll" /> <!--********************************************* ** Find the basic mod metadata |