|
| 1 | +/** |
| 2 | + * __ __ __ |
| 3 | + * ____/ /_ ____/ /______ _ ___ / /_ |
| 4 | + * / __ / / ___/ __/ ___/ / __ `/ __/ |
| 5 | + * / /_/ / (__ ) / / / / / /_/ / / |
| 6 | + * \__,_/_/____/_/ /_/ /_/\__, /_/ |
| 7 | + * / / |
| 8 | + * \/ |
| 9 | + * http://distriqt.com |
| 10 | + * |
| 11 | + * @author Michael (https://github.com/marchbold) |
| 12 | + * @created 18/5/21 |
| 13 | + */ |
| 14 | +package com.apm.client.commands.packages |
| 15 | +{ |
| 16 | + import com.apm.client.APM; |
| 17 | + import com.apm.client.commands.Command; |
| 18 | + import com.apm.client.commands.packages.processes.PackageContentCreateProcess; |
| 19 | + import com.apm.client.commands.packages.processes.PackageContentVerifyProcess; |
| 20 | + import com.apm.client.commands.packages.processes.PackageDependenciesVerifyProcess; |
| 21 | + import com.apm.client.commands.packages.processes.ViewPackageProcess; |
| 22 | + import com.apm.client.events.CommandEvent; |
| 23 | + import com.apm.client.logging.Log; |
| 24 | + import com.apm.client.processes.ProcessQueue; |
| 25 | + import com.apm.client.repositories.RepositoryResolver; |
| 26 | + import com.apm.data.packages.PackageDefinition; |
| 27 | + import com.apm.data.packages.PackageDefinitionFile; |
| 28 | + import com.apm.data.packages.PackageDependency; |
| 29 | + |
| 30 | + import flash.events.EventDispatcher; |
| 31 | + |
| 32 | + import flash.filesystem.File; |
| 33 | + |
| 34 | + |
| 35 | + public class PackageAddDependencyCommand extends EventDispatcher implements Command |
| 36 | + { |
| 37 | + |
| 38 | + //////////////////////////////////////////////////////// |
| 39 | + // CONSTANTS |
| 40 | + // |
| 41 | + |
| 42 | + private static const TAG:String = "PackageAddDependencyCommand"; |
| 43 | + |
| 44 | + public static const NAME:String = "package/add"; |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + //////////////////////////////////////////////////////// |
| 49 | + // VARIABLES |
| 50 | + // |
| 51 | + |
| 52 | + private var _parameters:Array; |
| 53 | + private var _queue:ProcessQueue; |
| 54 | + |
| 55 | + |
| 56 | + //////////////////////////////////////////////////////// |
| 57 | + // FUNCTIONALITY |
| 58 | + // |
| 59 | + |
| 60 | + public function PackageAddDependencyCommand() |
| 61 | + { |
| 62 | + super(); |
| 63 | + _queue = new ProcessQueue(); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + public function setParameters( parameters:Array ):void |
| 68 | + { |
| 69 | + _parameters = parameters; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public function get name():String |
| 74 | + { |
| 75 | + return NAME; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public function get category():String |
| 80 | + { |
| 81 | + return ""; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + public function get requiresNetwork():Boolean |
| 86 | + { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + public function get requiresProject():Boolean |
| 92 | + { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + public function get description():String |
| 98 | + { |
| 99 | + return "add a package dependency into a package under construction"; |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + public function get usage():String |
| 104 | + { |
| 105 | + return description + "\n" + |
| 106 | + "\n" + |
| 107 | + "apm package add <package_identifier> add a package dependency (there must be a package.json in the working dir)\n"; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + public function execute():void |
| 112 | + { |
| 113 | + if (_parameters == null || _parameters.length == 0) |
| 114 | + { |
| 115 | + APM.io.writeLine( "no search params provided" ); |
| 116 | + dispatchEvent( new CommandEvent( CommandEvent.PRINT_USAGE, NAME )); |
| 117 | + dispatchEvent( new CommandEvent( CommandEvent.COMPLETE, APM.CODE_ERROR )); |
| 118 | + return; |
| 119 | + } |
| 120 | + var path:String = ""; |
| 121 | + var identifier:String = _parameters[0]; |
| 122 | + |
| 123 | + APM.io.writeLine( "Adding package dependency: " + identifier ); |
| 124 | + |
| 125 | + var packageDir:File = new File( APM.config.workingDir + File.separator + path ); |
| 126 | + var packageFile:File = packageDir.resolvePath( PackageDefinitionFile.DEFAULT_FILENAME ); |
| 127 | + if (!packageFile.exists) |
| 128 | + { |
| 129 | + APM.io.writeLine( "no package definition file found" ); |
| 130 | + dispatchEvent( new CommandEvent( CommandEvent.PRINT_USAGE, NAME )); |
| 131 | + dispatchEvent( new CommandEvent( CommandEvent.COMPLETE, APM.CODE_ERROR )); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + APM.io.showSpinner( "Locating package " + identifier ); |
| 136 | + RepositoryResolver.repositoryForSource() |
| 137 | + .getPackageVersion( identifier, null, function ( success:Boolean, packageDef:PackageDefinition ):void |
| 138 | + { |
| 139 | + try |
| 140 | + { |
| 141 | + APM.io.stopSpinner( success, success ? "Found package " + packageDef.toString() : "Could not locate package " + identifier ); |
| 142 | + if (success && packageDef.versions.length > 0) |
| 143 | + { |
| 144 | + var packageDefinitionFile:PackageDefinitionFile = new PackageDefinitionFile().load( packageFile ); |
| 145 | + packageDefinitionFile.dependencies.push( |
| 146 | + new PackageDependency().fromObject( packageDef.toString() ) |
| 147 | + ); |
| 148 | + packageDefinitionFile.save(); |
| 149 | + dispatchEvent( new CommandEvent( CommandEvent.COMPLETE, APM.CODE_OK ) ); |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + dispatchEvent( new CommandEvent( CommandEvent.COMPLETE, APM.CODE_ERROR ) ); |
| 154 | + } |
| 155 | + } |
| 156 | + catch (e:Error) |
| 157 | + { |
| 158 | + Log.e( TAG, e ); |
| 159 | + dispatchEvent( new CommandEvent( CommandEvent.COMPLETE, APM.CODE_ERROR ) ); |
| 160 | + } |
| 161 | + } |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments