Created
June 12, 2015 12:34
-
-
Save YuukanOO/8bf57562069c3251c15c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Le fonctionnement est très simple, côté Program.cs du service (donc dans le Main): | |
/// | |
/// if (CLIServiceInstaller.Try("servicename")) | |
/// return; | |
/// | |
/// Les paramètres fournit sont les commandes prises en comptes parmis : | |
/// - user | |
/// - password | |
/// - servicename | |
/// - displayname | |
/// - description | |
/// | |
/// Ensuite à l'endroit où les ServiceInstaller et ServiceProcessInstaller sont déclarés, il suffit de les configurer via: | |
/// CLIServiceInstaller.SetServicePropertiesFromArguments(service, process); | |
/// | |
/// Pour finir, une fois votre service windows généré, il suffit d'appeler l'exécutable avec les commandes qui vont bien: | |
/// MonServiceWindows.exe /i /servicename NomAUtiliser | |
/// </summary> | |
public static class CLIServiceInstaller | |
{ | |
private static log4net.ILog _logger = log4net.LogManager.GetLogger(typeof(CLIServiceInstaller)); | |
/// <summary> | |
/// Récupère le chemin vers l'exe actuel | |
/// </summary> | |
public static string ExePath | |
{ | |
get | |
{ | |
Assembly entryAssembly = Assembly.GetEntryAssembly(); | |
if (entryAssembly != null) | |
return entryAssembly.Location; | |
return null; | |
} | |
} | |
/// <summary> | |
/// Procéde à l'installation / désinstallation en passant les arguments à installutil | |
/// </summary> | |
/// <param name="args"></param> | |
private static void Process(string[] args) | |
{ | |
try | |
{ | |
_logger.InfoFormat("Sending command to installutil: {0}", string.Join(" ", args)); | |
ManagedInstallerClass.InstallHelper(args); | |
_logger.Info("Command completed"); | |
} | |
catch (Exception e) | |
{ | |
_logger.Error(e.Message, e); | |
} | |
} | |
/// <summary> | |
/// Configure les services donnés suivant les paramètres passés par installutil | |
/// </summary> | |
/// <param name="installer"></param> | |
/// <param name="processInstaller"></param> | |
public static void SetServicePropertiesFromArguments( | |
System.ServiceProcess.ServiceInstaller installer, | |
ServiceProcessInstaller processInstaller) | |
{ | |
CLIArgumentsReader arguments = new CLIArgumentsReader(Environment.GetCommandLineArgs()); | |
if (processInstaller != null) | |
{ | |
processInstaller.Username = arguments["user"]; | |
processInstaller.Password = arguments["password"]; | |
} | |
if (installer != null) | |
{ | |
installer.ServiceName = arguments["servicename"]; | |
installer.DisplayName = arguments.TryGet("displayname", installer.ServiceName); | |
if (!string.IsNullOrEmpty(arguments["description"])) | |
installer.Description = arguments["description"]; | |
installer.StartType = System.ServiceProcess.ServiceStartMode.Automatic; | |
} | |
} | |
/// <summary> | |
/// Tente d'installer le service en parsant les arguments | |
/// </summary> | |
/// <param name="mandatoryParameters"></param> | |
/// <returns>true si il s'agissait bien d'une commande d'installation/désinstallation et que le programme doit se terminer, false sinon</returns> | |
public static bool Try(params string[] mandatoryParameters) | |
{ | |
CLIArgumentsReader arguments = new CLIArgumentsReader(Environment.GetCommandLineArgs()); | |
// On vérifie si les arguments obligatoires sont présents | |
foreach(string mandatoryParam in mandatoryParameters) | |
{ | |
if(arguments[mandatoryParam] == null) | |
return false; | |
} | |
List<string> installutilArgs = new List<string> { ExePath }; | |
foreach (DictionaryEntry argument in arguments.RawParameters) | |
{ | |
string toInsert = "/" + argument.Key + "=" + argument.Value; | |
string argKey = argument.Key.ToString(); | |
if (argKey == "u" || argKey == "uninstall" | |
|| argKey == "i" || argKey == "install") | |
toInsert = "/" + argument.Key; | |
installutilArgs.Insert(0, toInsert); | |
} | |
Process(installutilArgs.ToArray()); | |
return true; | |
} | |
} | |
/// <summary> | |
/// Permet le parsing des arguments de la ligne de commande | |
/// Récupérer depuis CodeProject: http://www.codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser | |
/// </summary> | |
public class CLIArgumentsReader | |
{ | |
private StringDictionary Parameters; | |
public CLIArgumentsReader(string[] Args) | |
{ | |
Parameters = new StringDictionary(); | |
Regex Spliter = new Regex(@"^-{1,2}|^/|=|:", | |
RegexOptions.IgnoreCase|RegexOptions.Compiled); | |
Regex Remover = new Regex(@"^['""]?(.*?)['""]?$", | |
RegexOptions.IgnoreCase|RegexOptions.Compiled); | |
string Parameter = null; | |
string[] Parts; | |
// Valid parameters forms: | |
// {-,/,--}param{ ,=,:}((",')value(",')) | |
// Examples: | |
// -param1 value1 --param2 /param3:"Test-:-work" | |
// /param4=happy -param5 '--=nice=--' | |
foreach(string Txt in Args) | |
{ | |
// Look for new parameters (-,/ or --) and a | |
// possible enclosed value (=,:) | |
Parts = Spliter.Split(Txt,3); | |
switch(Parts.Length){ | |
// Found a value (for the last parameter | |
// found (space separator)) | |
case 1: | |
if(Parameter != null) | |
{ | |
if(!Parameters.ContainsKey(Parameter)) | |
{ | |
Parts[0] = | |
Remover.Replace(Parts[0], "$1"); | |
Parameters.Add(Parameter, Parts[0]); | |
} | |
Parameter=null; | |
} | |
// else Error: no parameter waiting for a value (skipped) | |
break; | |
// Found just a parameter | |
case 2: | |
// The last parameter is still waiting. | |
// With no value, set it to true. | |
if(Parameter!=null) | |
{ | |
if(!Parameters.ContainsKey(Parameter)) | |
Parameters.Add(Parameter, "true"); | |
} | |
Parameter=Parts[1]; | |
break; | |
// Parameter with enclosed value | |
case 3: | |
// The last parameter is still waiting. | |
// With no value, set it to true. | |
if(Parameter != null) | |
{ | |
if(!Parameters.ContainsKey(Parameter)) | |
Parameters.Add(Parameter, "true"); | |
} | |
Parameter = Parts[1]; | |
// Remove possible enclosing characters (",') | |
if(!Parameters.ContainsKey(Parameter)) | |
{ | |
Parts[2] = Remover.Replace(Parts[2], "$1"); | |
Parameters.Add(Parameter, Parts[2]); | |
} | |
Parameter=null; | |
break; | |
} | |
} | |
// In case a parameter is still waiting | |
if(Parameter != null) | |
{ | |
if(!Parameters.ContainsKey(Parameter)) | |
Parameters.Add(Parameter, "true"); | |
} | |
} | |
public StringDictionary RawParameters | |
{ | |
get | |
{ | |
return Parameters; | |
} | |
} | |
public string this[string Param] | |
{ | |
get | |
{ | |
return (Parameters[Param]); | |
} | |
} | |
public string TryGet(string param, string replacement) | |
{ | |
string val = this[param]; | |
return string.IsNullOrEmpty(val) ? replacement : val; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment