Skip to content

Commit

Permalink
SccmTools.1.0.18114.11
Browse files Browse the repository at this point in the history
Implement command CreateDefinitionFromApplication.
  • Loading branch information
trondr committed Apr 24, 2018
1 parent 74aeaf1 commit 837e0d4
Show file tree
Hide file tree
Showing 32 changed files with 3,259 additions and 205 deletions.
6 changes: 3 additions & 3 deletions src/SccmTools.Gui/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
[assembly: AssemblyProduct("SccmTools.Gui")]
[assembly: AssemblyCopyright("Copyright © github.com.trondr 2018")]
[assembly: CLSCompliant(false)]
[assembly: AssemblyInformationalVersion("1.0.18110.10.1136d57")]
[assembly: AssemblyVersion("1.0.18110.10")]
[assembly: AssemblyFileVersion("1.0.18110.10")]
[assembly: AssemblyInformationalVersion("1.0.18114.11.74aeaf1")]
[assembly: AssemblyVersion("1.0.18114.11")]
[assembly: AssemblyFileVersion("1.0.18114.11")]


Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using LanguageExt;
using SccmTools.Library.Module.Common.IO;
using SccmTools.Library.Module.Services;
using Icon = Microsoft.ConfigurationManagement.ApplicationManagement.Icon;
Expand Down Expand Up @@ -124,6 +126,14 @@ private RegistryValue GetRegistryValue(string packageDefinitionFileName)

public void WritePackageDefinition(string packageDefinitionFileName, PackageDefinition packageDefinition)
{
if (!System.IO.File.Exists(packageDefinitionFileName))
{
CreateEmptyPackageDefinitionFile(packageDefinitionFileName).IfFail((exception) =>
{
typeof(PackageDefinitionProvider).Logger().Error($"Failed to write package definition file '{packageDefinitionFileName}' due to {exception.Message}");
throw exception;
});
}
SetValue(packageDefinitionFileName, "PDF", "Version", "2.0");
SetName(packageDefinitionFileName, packageDefinition.Name);
SetVersion(packageDefinitionFileName, packageDefinition.Version);
Expand All @@ -134,11 +144,68 @@ public void WritePackageDefinition(string packageDefinitionFileName, PackageDefi
SetInstallCommandLine(packageDefinitionFileName, packageDefinition.InstallCommandLine);
SetUnInstallCommandLine(packageDefinitionFileName, packageDefinition.UnInstallCommandLine);
//SetIcon(packageDefinitionFileName, packageDefinition.Icon);
SetMsiProductCode(packageDefinitionFileName, packageDefinition.MsiProductCode);
SetRegistryValue(packageDefinitionFileName, packageDefinition.RegistryValue);
if(!string.IsNullOrWhiteSpace(packageDefinition.MsiProductCode))
SetMsiProductCode(packageDefinitionFileName, packageDefinition.MsiProductCode);
if(packageDefinition.RegistryValue != null)
SetRegistryValue(packageDefinitionFileName, packageDefinition.RegistryValue);
//SetContentDirectory(packageDefinitionFileName,packageDefinition.ContentDirectory);
}

private Result<string> CreateEmptyPackageDefinitionFile(string packageDefinitionFileName)
{
if(System.IO.File.Exists(packageDefinitionFileName))
return new Result<string>(new SccmToolsException($"Package definition file '{packageDefinitionFileName}' allready exists."));
try
{
using (var sw = new StreamWriter(packageDefinitionFileName))
{
sw.Write(GetEmptyPackageDefinitionFileContent());
}
return new Result<string>(packageDefinitionFileName);
}
catch (Exception e)
{
return new Result<string>(new SccmToolsException($"Failed to write empty package definition file due to {e.Message}", e));
}
}

private string GetEmptyPackageDefinitionFileContent()
{
return @"[PDF]
Version=2.0
[Package Definition]
Name=
Version=
Publisher=
Language=
Comment=
Programs=INSTALL,UNINSTALL
[INSTALL]
Name=INSTALL
CommandLine=
CanRunWhen=AnyUserStatus
UserInputRequired=False
AdminRightsRequired=True
UseInstallAccount=True
Run=Minimized
Icon=App.ico
Comment=
[UNINSTALL]
Name=UNINSTALL
CommandLine=
CanRunWhen=AnyUserStatus
UserInputRequired=False
AdminRightsRequired=True
UseInstallAccount=True
Run=Minimized
Icon=App.ico
Comment=
";
}

private string GetName(string packageDefinitionFileName)
{
var name = GetValue(packageDefinitionFileName, "Package Definition", "Name", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.ConfigurationManagement.ApplicationManagement;
using NCmdLiner;
using SccmTools.Library.Module.Commands.CreateApplication;
using SccmTools.Library.Module.Services;

namespace SccmTools.Library.Module.Commands.CreateDefinitionFromApplication
{
public class CreateDefinitionFromApplicationCommandProvider : ICreateDefinitionFromApplicationCommandProvider
{
private readonly ISccmApplicationProvider _sccmApplicationProvider;
private readonly IPackageDefinitionProvider _packageDefinitionProvider;

public CreateDefinitionFromApplicationCommandProvider(ISccmApplicationProvider sccmApplicationProvider, IPackageDefinitionProvider packageDefinitionProvider)
{
_sccmApplicationProvider = sccmApplicationProvider;
_packageDefinitionProvider = packageDefinitionProvider;
}

public Result<int> CreateDefinitionFromApplication(string applicationName, string applicationVersion)
{
var logger = typeof(CreateDefinitionFromApplicationCommandProvider).Logger();
var configurationManagerConsoleIsInstalled = F.GetAdminConsoleBinPath().Match(path => true, exception =>
{
logger.Error(exception.Message);
return false;
});
if (!configurationManagerConsoleIsInstalled)
return Result.Ok(1);

var applications = _sccmApplicationProvider.FindApplication(applicationName, applicationVersion).ToList();
if (applications.Count == 0)
{
logger.Error($"Application '{applicationName}-{applicationVersion}' does not exists in SCCM");
return Result.Ok(1);
}
if (applications.Count > 1)
{
logger.Error($"More than one application '{applicationName}-{applicationVersion}' exist in SCCM. Duplicates are not supported.");
return Result.Ok(1);
}
var application = applications[0];
var deploymentTypes = application.DeploymentTypes;
if (deploymentTypes.Count == 0)
{
logger.Error($"No deployment type found on application '{applicationName}-{applicationVersion}'.");
return Result.Ok(1);
}
if (deploymentTypes.Count > 1)
{
logger.Error($"More than 1 deployment type found on application '{applicationName}-{applicationVersion}'. Only one deployment type is supported.");
return Result.Ok(1);
}
var packageDefinition = GetPackageDefinitionFromDeploymentType(application);
var result = WritePackageDefinition(packageDefinition);
var exitCode = result.Match(definition => Result.Ok(0), Result.Fail<int>);
return exitCode;
}

private LanguageExt.Result<PackageDefinition> WritePackageDefinition(LanguageExt.Result<PackageDefinition> packageDefinition)
{
return packageDefinition.Match(definition => WritePackageDefinition(definition), exception => packageDefinition);
}

private LanguageExt.Result<PackageDefinition> WritePackageDefinition(PackageDefinition packageDefinition)
{
if (packageDefinition == null) throw new ArgumentNullException(nameof(packageDefinition));
try
{
var packageDefinitionFilePath = Path.Combine(packageDefinition.ContentDirectory, "PackageDefinition.sms");
if(System.IO.File.Exists(packageDefinitionFilePath))
return new LanguageExt.Result<PackageDefinition>(new SccmToolsException($"Package definition file allready exists for application '{packageDefinition.Name}-{packageDefinition.Version}'"));
_packageDefinitionProvider.WritePackageDefinition(packageDefinitionFilePath, packageDefinition);
return new LanguageExt.Result<PackageDefinition>(packageDefinition);
}
catch (Exception e)
{
return new LanguageExt.Result<PackageDefinition>(new SccmToolsException($"Failed to write package definition to file due to {e.Message}", e));
}
}

private LanguageExt.Result<PackageDefinition> GetPackageDefinitionFromDeploymentType(Application application)
{
try
{
var installer = application.DeploymentTypes[0].Installer;
var scriptInstaller = (ScriptInstaller)installer;
var packageDefinition = new PackageDefinition(
application.Title,
application.SoftwareVersion,
application.Publisher,
application.Description,
application.DisplayInfo[0].Language,
scriptInstaller.InstallCommandLine,
scriptInstaller.UninstallCommandLine,
application.DisplayInfo[0].Icon,
string.Empty,
null,
false,
application.DeploymentTypes[0].Installer.Contents[0].Location,
new Dependency[] { });
return new LanguageExt.Result<PackageDefinition>(packageDefinition);
}
catch (Exception e)
{
return new LanguageExt.Result<PackageDefinition>(new SccmToolsException($"Failed to get package definition from application due to {e.Message}",e));
}
}

private string GetContentLocationPath(DeploymentType deploymentType)
{
var contentLocationPath = deploymentType.Installer.Contents[0].Location;
return contentLocationPath;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


using NCmdLiner;

namespace SccmTools.Library.Module.Commands.CreateDefinitionFromApplication
{
public interface ICreateDefinitionFromApplicationCommandProvider
{
Result<int> CreateDefinitionFromApplication(string applicationName, string applicationVersion);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SccmTools.Library.Module.Commands.CreateDefinitionFromApplication
{
public interface ICreateDefinitionFromApplicationCommandProviderFactory
{
ICreateDefinitionFromApplicationCommandProvider GetCreateDefinitionFromApplicationCommandProvider();

void Release(ICreateDefinitionFromApplicationCommandProvider createDefinitionFromApplicationCommandProvider);
}
}
6 changes: 3 additions & 3 deletions src/SccmTools.Library/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
[assembly: AssemblyProduct("SccmTools.Library")]
[assembly: AssemblyCopyright("Copyright © github.com.trondr 2018")]
[assembly: CLSCompliant(false)]
[assembly: AssemblyInformationalVersion("1.0.18110.10.1136d57")]
[assembly: AssemblyVersion("1.0.18110.10")]
[assembly: AssemblyFileVersion("1.0.18110.10")]
[assembly: AssemblyInformationalVersion("1.0.18114.11.74aeaf1")]
[assembly: AssemblyVersion("1.0.18114.11")]
[assembly: AssemblyFileVersion("1.0.18114.11")]


6 changes: 6 additions & 0 deletions src/SccmTools.Library/SccmTools.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\tools\Sccm\Microsoft.Deployment.WindowsInstaller.dll</HintPath>
</Reference>
<Reference Include="NCmdLiner, Version=2.0.18110.340, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\tools\NuGetPackages\NCmdLiner.2.0.18110.340\lib\net452\NCmdLiner.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\tools\NuGetPackages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -152,6 +155,9 @@
<Compile Include="Module\Commands\CreateApplication\IPackageDefinitionFileProvider.cs" />
<Compile Include="Module\Commands\CreateApplication\PackageDefinitionFileProvider.cs" />
<Compile Include="Module\Commands\CreateApplication\RealFile.cs" />
<Compile Include="Module\Commands\CreateDefinitionFromApplication\CreateDefinitionFromApplicationCommandProvider.cs" />
<Compile Include="Module\Commands\CreateDefinitionFromApplication\ICreateDefinitionFromApplicationCommandProvider.cs" />
<Compile Include="Module\Commands\CreateDefinitionFromApplication\ICreateDefinitionFromApplicationCommandProviderFactory.cs" />
<Compile Include="Module\Common\Caching\CacheProvider.cs" />
<Compile Include="Module\Common\Caching\CacheValidator.cs" />
<Compile Include="Module\Common\Caching\CacheValue.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/SccmTools.Library/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<package id="ini-parser" version="2.3.0" targetFramework="net452" />
<package id="LanguageExt.Core" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.CSharp" version="4.4.1" targetFramework="net452" />
<package id="NCmdLiner" version="2.0.18110.340" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
<package id="System.Reactive" version="3.1.1" targetFramework="net452" />
<package id="System.Reactive.Core" version="3.1.1" targetFramework="net452" />
Expand Down
Loading

0 comments on commit 837e0d4

Please sign in to comment.