Skip to content

Commit

Permalink
closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
dazinator committed Jul 19, 2015
1 parent 7f27836 commit 48e8970
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 490 deletions.
155 changes: 121 additions & 34 deletions src/DnnPackager.Tasks/CreateDnnExtensionInstallationZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,22 @@
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DnnPackager.Util;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
using Microsoft.Build.Utilities;

namespace DnnPackager.Tasks
{


public class CreateDnnExtensionInstallationZip : AbstractTask
{
private IFileSystem _fileSystem;

public CreateDnnExtensionInstallationZip()
: this(new PhysicalFileSystem())
{
public const string ReleaseNotesFileName = "ReleaseNotes.txt";
public const string IntermediateOutputFolderName = "DnnPackager";

}

public CreateDnnExtensionInstallationZip(IFileSystem fileSystem)
public CreateDnnExtensionInstallationZip()
{
if (fileSystem == null)
{
throw new ArgumentNullException("fileSystem");
}

_fileSystem = fileSystem;
}

[Required]
Expand Down Expand Up @@ -61,6 +51,8 @@ public CreateDnnExtensionInstallationZip(IFileSystem fileSystem)
[Required]
public ITaskItem[] Symbols { get; set; }

public bool DebugSymbols { get; set; }

/// <summary>
/// Used to output the built zip install package.
/// </summary>
Expand All @@ -69,39 +61,110 @@ public CreateDnnExtensionInstallationZip(IFileSystem fileSystem)

public override bool ExecuteTask()
{
var packagingDir = CreateEmptyOutputDirectory("dnnpackager");

var packagingDir = CreateEmptyOutputDirectory(IntermediateOutputFolderName);
string outputZipFileName = Path.Combine(packagingDir, "resources.zip");
CreateResourcesZip(outputZipFileName);
CreateResourcesZip(outputZipFileName);

//todo: finish below

// copy the manifest to packaging dir
CopyFile(ManifestFilePath, packagingDir);

// Ensure packagingdir\bin dir
string binFolder = Path.Combine(packagingDir, "bin");
EnsureEmptyDirectory(binFolder);

// copy assemblies to packagingdir\bin
// copy assemblies to packagingdir\bin
CopyFileTaskItems(ProjectDirectory, Assemblies, binFolder);

// copy symbols to packagingdir\bin
if (DebugSymbols)
{
CopyFileTaskItems(ProjectDirectory, Symbols, binFolder, true);
}

// copy AdditionalFiles to packagingdir (keeping same relative path from new parent dir)
CopyFileTaskItems(ProjectDirectory, AdditionalFiles, packagingDir, false, true);

// find any
// .sqldataprovider files
// .lic files
// "ReleaseNotes.txt" file
// and copy them to the same relative directory in the packaging dir.
ITaskItem[] specialPackageContentFiles =
FindContentFiles(t =>
Path.GetExtension(t.ItemSpec).ToLowerInvariant() == ".sqldataprovider" ||
Path.GetExtension(t.ItemSpec).ToLowerInvariant() == ".lic" ||
Path.GetFileName(t.ItemSpec).ToLowerInvariant() == ReleaseNotesFileName.ToLowerInvariant()
);
CopyFileTaskItems(ProjectDirectory, specialPackageContentFiles, packagingDir, false, true);

// find any .sqldataprovider files in project and copy them to packagingdir (keeping same relative path from new parent dir)

// find any .lic files in project and copy them to packagingdir (keeping same relative path from new parent dir)

// find any ReleaseNotes.txt file in project and copy it to packagingdir (keeping same relative path from parent dir)


// otpional: check that if a lic file is referenced in manifest that it exists in packagingdir
// otpional: check that if a releasenotes file is referenced in manifest that it exists in packagingdir
// otpional: run variable substitution against manifest?
// otpional: ensure manifest has a ResourceFile component that references Resources.zip?

// zip up packagingdir to OutputDirectory\OutputZipFileName
throw new NotImplementedException();
// zip up packagingdir to OutputDirectory\OutputZipFileName
string installZipFileName = Path.Combine(OutputDirectory, OutputZipFileName);
CompressFolder(packagingDir, installZipFileName);

InstallPackage = new TaskItem(installZipFileName);
return true;
}

private ITaskItem[] FindContentFiles(Predicate<ITaskItem> filter)
{
var items = ResourcesZipContent.Where(t => filter(t)).ToArray();
return items;
}

private void CopyFileTaskItems(string baseDir, ITaskItem[] taskItems, string destinationFolder, bool skipWhenNotExists = false, bool keepRelativePath = false)
{
foreach (var item in taskItems)
{
var sourceFilePath = Path.Combine(baseDir, item.ItemSpec);
sourceFilePath = Path.GetFullPath(sourceFilePath);
string targetDir = destinationFolder;

if (keepRelativePath)
{
// rather than copy the source files directly into the destination folder,
// if the source file is in: baseDir/somefolder/someotherFolder
// then it should end up in destinationFolder/somefolder/someotherFolder
targetDir = Path.GetDirectoryName(Path.GetFullPath(Path.Combine(destinationFolder, item.ItemSpec)));

}
CopyFile(sourceFilePath, targetDir, skipWhenNotExists);
}
}

private void CopyFile(string sourceFile, string targetDir, bool skipIfNotExists = false)
{
var fileInfo = new FileInfo(sourceFile);
if (!fileInfo.Exists)
{
if (skipIfNotExists)
{
return;
}
throw new FileNotFoundException("Unable to find file.", sourceFile);
}

var sourceFileName = Path.GetFileName(sourceFile);

if (!Directory.Exists(targetDir))
{
Directory.CreateDirectory(targetDir);
}

var targetFileName = Path.Combine(targetDir, sourceFileName);
File.Copy(sourceFile, targetFileName);
}

public void CreateResourcesZip(string outputZipFileName)
{
// var outputFileName = Path.Combine(outputPathForZip, OutputZipFileName);
// var outputFileName = Path.Combine(outputPathForZip, OutputZipFileName);
using (var fsOut = File.Create(outputZipFileName))
{
using (var zipStream = new ZipOutputStream(fsOut))
Expand All @@ -112,7 +175,7 @@ public void CreateResourcesZip(string outputZipFileName)
zipStream.IsStreamOwner = true; // Makes the Close also Close the underlying stream
zipStream.Close();
}
}
}
}

private void CompressFileItems(string baseDir, ZipOutputStream zipStream, ITaskItem[] items)
Expand All @@ -129,7 +192,7 @@ private void CompressFileItems(string baseDir, ZipOutputStream zipStream, ITaskI
{
LogMessage("The source file '" + sourceFilePath + "' does not exist, so it will not be included in the package", MessageImportance.High);
continue;
}
}

string entryName = sourceFilePath.Substring(folderOffset); // Makes the name in zip based on the folder
entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction
Expand Down Expand Up @@ -160,17 +223,41 @@ private void CompressFileItems(string baseDir, ZipOutputStream zipStream, ITaskI
}
}

private void CompressFolder(string sourceDir, string outputFileName)
{
FastZip fastZip = new FastZip();
bool recurse = true; // Include all files by recursing through the directory structure
string filter = null; // Dont filter any files at all
fastZip.CreateZip(outputFileName, sourceDir, recurse, filter);
}

private string CreateEmptyOutputDirectory(string name)
{
var temp = Path.Combine(ProjectDirectory, "obj", name);
LogMessage("Create directory: " + temp, MessageImportance.Low);

_fileSystem.PurgeDirectory(temp, DeletionOptions.TryThreeTimes);
_fileSystem.EnsureDirectoryExists(temp);
_fileSystem.EnsureDiskHasEnoughFreeSpace(temp);
EnsureEmptyDirectory(temp);
//_fileSystem.EnsureDiskHasEnoughFreeSpace(temp);
return temp;
}
}

private void EnsureEmptyDirectory(string dirPath)
{
if (Directory.Exists(dirPath))
{
System.IO.DirectoryInfo dir = new DirectoryInfo(dirPath);
foreach (FileInfo file in dir.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo d in dir.GetDirectories())
{
d.Delete(true);
}
}

Directory.CreateDirectory(dirPath);
LogMessage("Created directory: " + dirPath, MessageImportance.Low);
}

}
}
3 changes: 1 addition & 2 deletions src/DnnPackager.Tasks/DnnPackager.Tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<HintPath>..\..\lib\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Build.Utilities.v4.0" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
Expand All @@ -50,8 +51,6 @@
<Compile Include="CreateDnnExtensionInstallationZip.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadPackageInfoFromManifest.cs" />
<Compile Include="Util\IFileSystem.cs" />
<Compile Include="Util\XmlElementExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DnnPackager\DnnPackager.csproj">
Expand Down
Loading

2 comments on commit 48e8970

@dazinator
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity DnnPackager :: Continuos Build 17 is now running

@dazinator
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity DnnPackager :: Continuos Build 1.1.0-unstable.4+4 outcome was SUCCESS
Summary: Running Build time: 00:00:12

Please sign in to comment.