move create-zip task into project code

This commit is contained in:
Jesse Plamondon-Willard 2017-10-08 01:27:52 -04:00
parent af68910685
commit ddad601de3
4 changed files with 120 additions and 122 deletions

View File

@ -16,7 +16,7 @@
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
@ -24,21 +24,31 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>
<Optimize>true</Optimize> <Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath> <OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.Build" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Web.Extensions" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Tasks\CreateModReleaseZip.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="assets\nuget-icon.pdn" /> <None Include="assets\nuget-icon.pdn" />
<None Include="build\smapi.targets" /> <None Include="build\smapi.targets">
<None Include="package.nuspec" /> <SubType>Designer</SubType>
</None>
<None Include="package.nuspec">
<SubType>Designer</SubType>
</None>
<None Include="README.md" /> <None Include="README.md" />
<None Include="release-notes.md" /> <None Include="release-notes.md" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,103 @@
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;
namespace StardewModdingAPI.ModBuildConfig.Tasks
{
/// <summary>A build task which packs mod files into a conventional release zip.</summary>
public class CreateModReleaseZip : Task
{
/*********
** Accessors
*********/
/// <summary>The mod files to pack.</summary>
[Required]
public ITaskItem[] Files { get; set; }
/// <summary>The name of the mod.</summary>
[Required]
public string ModName { get; set; }
/// <summary>The absolute or relative path to the folder which should contain the generated zip file.</summary>
[Required]
public string OutputFolderPath { get; set; }
/*********
** Public methods
*********/
public override bool Execute()
{
try
{
// create output path if needed
Directory.CreateDirectory(this.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(this.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 this.Files)
{
// get file info
string filePath = file.ItemSpec;
string entryName = this.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)
{
this.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 this.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);
}
}
}

View File

@ -1,124 +1,8 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--********************************************* <!--*********************************************
** Define build tasks ** Import build tasks
**********************************************--> **********************************************-->
<!--###### <UsingTask TaskName="CreateModReleaseZip" AssemblyFile="StardewModdingAPI.ModBuildConfig.dll" />
## 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>
<!--********************************************* <!--*********************************************
** Find the basic mod metadata ** Find the basic mod metadata

View File

@ -17,6 +17,7 @@
</metadata> </metadata>
<files> <files>
<file src="build/smapi.targets" target="build/Pathoschild.Stardew.ModBuildConfig.targets" /> <file src="build/smapi.targets" target="build/Pathoschild.Stardew.ModBuildConfig.targets" />
<file src="bin/StardewModdingAPI.ModBuildConfig.dll" target="build/StardewModdingAPI.ModBuildConfig.dll" />
<file src="readme.md" /> <file src="readme.md" />
</files> </files>
</package> </package>