Skip to content

Commit

Permalink
Merge #2264 Add Cmdline import command
Browse files Browse the repository at this point in the history
  • Loading branch information
politas committed Jan 30, 2018
2 parents dfd03d7 + 129b327 commit 472c167
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- [GUI] Add status and progress bar at the bottom of the window (#2245 by: HebaruSan; reviewed: Olympic1)
- [GUI] Add import downloads menu item to GUI (#2246 by: HebaruSan; reviewed: politas)
- [Core] Accept header and infrastructure for auth tokens (#2263 by: HebaruSan; reviewed: dbent)
- [CLI] Add Cmdline import command (#2264 by: HebaruSan; reviewed: politas)

### Bugfixes

Expand Down
106 changes: 106 additions & 0 deletions Cmdline/Action/Import.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.IO;
using System.Collections.Generic;
using log4net;

namespace CKAN.CmdLine
{

/// <summary>
/// Handler for "ckan import" command.
/// Imports manually downloaded ZIP files into the cache.
/// </summary>
public class Import : ICommand
{

/// <summary>
/// Initialize the command
/// </summary>
/// <param name="user">IUser object for user interaction</param>
public Import(IUser user)
{
this.user = user;
}

/// <summary>
/// Execute an import command
/// </summary>
/// <param name="ksp">Game instance into which to import</param>
/// <param name="options">Command line parameters from the user</param>
/// <returns>
/// Process exit code
/// </returns>
public int RunCommand(CKAN.KSP ksp, object options)
{
try
{
ImportOptions opts = options as ImportOptions;
HashSet<FileInfo> toImport = GetFiles(opts);
if (toImport.Count < 1)
{
user.RaiseMessage("Usage: ckan import path [path2, ...]");
return Exit.ERROR;
}
else
{
log.InfoFormat("Importing {0} files", toImport.Count);
List<string> toInstall = new List<string>();
ModuleInstaller inst = ModuleInstaller.GetInstance(ksp, user);
inst.ImportFiles(toImport, user, id => toInstall.Add(id), !opts.Headless);
if (toInstall.Count > 0)
{
inst.InstallList(
toInstall,
new RelationshipResolverOptions()
);
}
return Exit.OK;
}
}
catch (Exception ex)
{
user.RaiseError("Import error: {0}", ex.Message);
return Exit.ERROR;
}
}

private HashSet<FileInfo> GetFiles(ImportOptions options)
{
HashSet<FileInfo> files = new HashSet<FileInfo>();
foreach (string filename in options.paths)
{
if (Directory.Exists(filename))
{
// Import everything in this folder
log.InfoFormat("{0} is a directory", filename);
foreach (string dirfile in Directory.EnumerateFiles(filename))
{
AddFile(files, dirfile);
}
}
else
{
AddFile(files, filename);
}
}
return files;
}

private void AddFile(HashSet<FileInfo> files, string filename)
{
if (File.Exists(filename))
{
log.InfoFormat("Attempting import of {0}", filename);
files.Add(new FileInfo(filename));
}
else
{
user.RaiseMessage("File not found: {0}", filename);
}
}

private readonly IUser user;
private static readonly ILog log = LogManager.GetLogger(typeof(Import));
}

}
1 change: 1 addition & 0 deletions Cmdline/CKAN-cmdline.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Action\Compare.cs" />
<Compile Include="Action\CompatSubCommand.cs" />
<Compile Include="Action\ICommand.cs" />
<Compile Include="Action\Import.cs" />
<Compile Include="Action\Install.cs" />
<Compile Include="Action\ISubCommand.cs" />
<Compile Include="Action\KSP.cs" />
Expand Down
3 changes: 3 additions & 0 deletions Cmdline/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ private static int RunSimpleAction(Options cmdline, CommonOptions options, strin
Scan(GetGameInstance(manager), user, cmdline.action);
return (new Upgrade(user)).RunCommand(GetGameInstance(manager), cmdline.options);

case "import":
return (new Import(user)).RunCommand(GetGameInstance(manager), options);

case "clean":
return Clean(GetGameInstance(manager));

Expand Down
12 changes: 12 additions & 0 deletions Cmdline/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ internal class Actions : VerbCommandOptions
[VerbOption("remove", HelpText = "Remove an installed mod")]
public RemoveOptions Remove { get; set; }

[VerbOption("import", HelpText = "Import manually downloaded mods")]
public ImportOptions Import { get; set; }

[VerbOption("scan", HelpText = "Scan for manually installed KSP mods")]
public ScanOptions Scan { get; set; }

Expand Down Expand Up @@ -132,6 +135,9 @@ public string GetUsage(string verb)
case "compare":
ht.AddPreOptionsLine($"Usage: ckan {verb} [options] version1 version2");
break;
case "import":
ht.AddPreOptionsLine($"Usage: ckan {verb} [options] paths");
break;

// Now the commands with only --flag type options
case "gui":
Expand Down Expand Up @@ -433,6 +439,12 @@ internal class RemoveOptions : InstanceSpecificOptions
public bool rmall { get; set; }
}

internal class ImportOptions : InstanceSpecificOptions
{
[ValueList(typeof(List<string>))]
public List<string> paths { get; set; }
}

internal class ShowOptions : InstanceSpecificOptions
{
[ValueOption(0)] public string Modname { get; set; }
Expand Down
7 changes: 4 additions & 3 deletions Core/ModuleInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,8 @@ private void DownloadModules(IEnumerable<CkanModule> mods, IDownloader downloade
/// <param name="files">Set of files to import</param>
/// <param name="user">Object for user interaction</param>
/// <param name="installMod">Function to call to mark a mod for installation</param>
public void ImportFiles(HashSet<FileInfo> files, IUser user, Action<string> installMod)
/// <param name="allowDelete">True to ask user whether to delete imported files, false to leave the files as is</param>
public void ImportFiles(HashSet<FileInfo> files, IUser user, Action<string> installMod, bool allowDelete = true)
{
Registry registry = registry_manager.registry;
HashSet<string> installable = new HashSet<string>();
Expand Down Expand Up @@ -1134,15 +1135,15 @@ public void ImportFiles(HashSet<FileInfo> files, IUser user, Action<string> inst
}
++i;
}
if (installable.Count > 0 && user.RaiseYesNoDialog($"Install {installable.Count} compatible imported mods?"))
if (installable.Count > 0 && user.RaiseYesNoDialog($"Install {installable.Count} compatible imported mods in game instance {ksp.Name} ({ksp.GameDir()})?"))
{
// Install the imported mods
foreach (string identifier in installable)
{
installMod(identifier);
}
}
if (user.RaiseYesNoDialog($"Import complete. Delete {deletable.Count} old files?"))
if (allowDelete && deletable.Count > 0 && user.RaiseYesNoDialog($"Import complete. Delete {deletable.Count} old files?"))
{
// Delete old files
foreach (FileInfo f in deletable)
Expand Down
2 changes: 1 addition & 1 deletion Core/Net/NetModuleCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public string Store(CkanModule module, string path, string description = null, b
}

// If no exceptions, then everything is fine
return cache.Store(module.download, path, description, move);
return cache.Store(module.download, path, description ?? module.StandardName(), move);
}

private NetFileCache cache;
Expand Down

0 comments on commit 472c167

Please sign in to comment.