private static string ParseProjectProperties(BridgeOptions bridgeOptions, string parameters, ILogger logger) { var properties = new ProjectProperties(); bridgeOptions.ProjectProperties = properties; if (string.IsNullOrWhiteSpace(parameters)) { return(null); } if (parameters != null && parameters.Length > 1 && parameters[0] == '"' && parameters.Last() == '"') { parameters = parameters.Trim('"'); } var settings = new Dictionary <string, string>(); var splitParameters = parameters.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var pair in splitParameters) { if (pair == null) { continue; } var parts = pair.Split(new char[] { ':' }, 2); if (parts.Length < 2) { logger.Warn("Skipped " + pair + " when parsing --settings as it is not well-formed like name:value"); continue; } var name = parts[0].Trim(); if (string.IsNullOrWhiteSpace(name)) { logger.Warn("Skipped " + pair + " when parsing --settings as name is empty in name:value"); continue; } string value; if (settings.ContainsKey(name)) { value = settings[name]; logger.Warn("Skipped " + pair + " when parsing --settings as it already found in " + name + ":" + value); continue; } value = parts[1]; if (value != null && value.Length > 1 && (value[0] == '"' || value.Last() == '"')) { value = value.Trim('"'); } settings.Add(name, value); } try { properties.SetValues(settings); } catch (ArgumentException ex) { return(ex.Message); } return(null); }
public static BridgeOptions GetBridgeOptionsFromCommandLine(string[] args, ILogger logger) { var bridgeOptions = new BridgeOptions(); bridgeOptions.Name = ""; bridgeOptions.ProjectProperties = new ProjectProperties(); // options -c, -P and -D have priority over -S string configuration = null; var hasPriorityConfiguration = false; string platform = null; var hasPriorityPlatform = false; string defineConstants = null; var hasPriorityDefineConstants = false; int i = 0; while (i < args.Length) { switch (args[i]) { // backwards compatibility -- now is non-switch argument to builder case "-p": case "-project": case "--project": if (bridgeOptions.Lib != null) { logger.Error("Error: Project and assembly file specification is mutually exclusive."); return(null); } ; bridgeOptions.ProjectLocation = args[++i]; break; case "-b": case "-bridge": // backwards compatibility case "--bridge": bridgeOptions.BridgeLocation = args[++i]; break; case "-o": case "-output": // backwards compatibility case "--output": bridgeOptions.OutputLocation = args[++i]; break; case "-c": case "-cfg": // backwards compatibility case "-configuration": // backwards compatibility case "--configuration": configuration = args[++i]; hasPriorityConfiguration = true; break; case "-P": case "--platform": platform = args[++i]; hasPriorityPlatform = true; break; case "-def": // backwards compatibility case "-D": case "-define": // backwards compatibility case "--define": defineConstants = args[++i]; hasPriorityDefineConstants = true; break; case "-rebuild": // backwards compatibility case "--rebuild": case "-r": bridgeOptions.Rebuild = true; break; case "-nocore": // backwards compatibility case "--nocore": bridgeOptions.ExtractCore = false; break; case "-s": case "-src": // backwards compatibility case "--source": bridgeOptions.Sources = args[++i]; break; case "-S": case "--settings": var error = ParseProjectProperties(bridgeOptions, args[++i], logger); if (error != null) { logger.Error("Invalid argument --setting(-S): " + args[i]); logger.Error(error); return(null); } break; case "-f": case "-folder": // backwards compatibility case "--folder": bridgeOptions.Folder = Path.Combine(Environment.CurrentDirectory, args[++i]); break; case "-R": case "-recursive": // backwards compatibility case "--recursive": bridgeOptions.Recursive = true; break; case "-lib": // backwards compatibility -- now is non-switch argument to builder if (bridgeOptions.ProjectLocation != null) { logger.Error("Error: Project and assembly file specification is mutually exclusive."); return(null); } bridgeOptions.Lib = args[++i]; break; case "-h": case "--help": ShowHelp(logger); bridgeOptions.Help = true; return(bridgeOptions); // success. Asked for help. Help provided. case "-notimestamp": case "--notimestamp": bridgeOptions.NoTimeStamp = true; break; #if DEBUG case "-debug": case "--debug": case "-attachdebugger": case "--attachdebugger": case "-d": System.Diagnostics.Debugger.Launch(); break; #endif case "--": // stop reading commandline arguments // Only non-hyphen commandline argument accepted is the file name of the project or // assembly file, so if not provided already, when this option is specified, check if // it is still needed and bind the file to the correct location if (i < (args.Length - 1)) { // don't care about success. If not set already, then try next cmdline argument // as the file parameter and ignore following arguments, if any. BindCmdArgumentToOption(args[i + 1], bridgeOptions, logger); } i = args.Length; // move to the end of arguments list break; default: // If this argument does not look like a cmdline switch and // neither backwards -project nor -lib were specified if (!BindCmdArgumentToOption(args[i], bridgeOptions, logger)) { logger.Error("Invalid argument: " + args[i]); return(null); } break; } i++; } if (hasPriorityConfiguration) { bridgeOptions.ProjectProperties.Configuration = configuration; } if (hasPriorityPlatform) { bridgeOptions.ProjectProperties.Platform = platform; } if (hasPriorityDefineConstants) { bridgeOptions.ProjectProperties.DefineConstants = defineConstants; } if (bridgeOptions.ProjectLocation == null && bridgeOptions.Lib == null) { var folder = bridgeOptions.Folder ?? Environment.CurrentDirectory; var csprojs = new string[] { }; try { csprojs = Directory.GetFiles(folder, "*.csproj", SearchOption.TopDirectoryOnly); } catch (Exception ex) { logger.Error(ex.ToString()); } if (csprojs.Length > 1) { logger.Error("Could not default to a csproj because multiple were found:"); logger.Info(string.Join(", ", csprojs.Select(path => Path.GetFileName(path)))); return(null); // error: arguments not provided, so can't guess what to do } if (csprojs.Length == 0) { logger.Warn("Could not default to a csproj because none were found."); logger.Error("Error: Project or assembly file name must be specified."); return(null); } var csproj = csprojs[0]; bridgeOptions.ProjectLocation = csproj; logger.Info("Defaulting Project Location to " + csproj); } if (string.IsNullOrEmpty(bridgeOptions.OutputLocation)) { bridgeOptions.OutputLocation = !string.IsNullOrWhiteSpace(bridgeOptions.ProjectLocation) ? Path.GetFileNameWithoutExtension(bridgeOptions.ProjectLocation) : bridgeOptions.Folder; } if (bridgeOptions.IsFolderMode) { bridgeOptions.DefaultFileName = Path.GetFileNameWithoutExtension(bridgeOptions.Lib); bridgeOptions.ProjectProperties.AssemblyName = bridgeOptions.DefaultFileName; } else { bridgeOptions.DefaultFileName = Path.GetFileName(bridgeOptions.OutputLocation); } if (string.IsNullOrWhiteSpace(bridgeOptions.DefaultFileName)) { bridgeOptions.DefaultFileName = Path.GetFileName(bridgeOptions.OutputLocation); } return(bridgeOptions); }