Changeset View
Changeset View
Standalone View
Standalone View
src/workflow/ArcanistUploadWorkflow.php
| Show All 17 Lines | return $this->newWorkflowInformation() | ||||
| ->addExample(pht('**upload** [__options__] -- __file__ [__file__ ...]')) | ->addExample(pht('**upload** [__options__] -- __file__ [__file__ ...]')) | ||||
| ->setHelp($help); | ->setHelp($help); | ||||
| } | } | ||||
| public function getWorkflowArguments() { | public function getWorkflowArguments() { | ||||
| return array( | return array( | ||||
| $this->newWorkflowArgument('json') | $this->newWorkflowArgument('json') | ||||
| ->setHelp(pht('Output upload information in JSON format.')), | ->setHelp(pht('Output upload information in JSON format.')), | ||||
| $this->newWorkflowArgument('browse') | |||||
| ->setHelp( | |||||
| pht( | |||||
| 'After the upload completes, open the files in a web browser.')), | |||||
| $this->newWorkflowArgument('temporary') | $this->newWorkflowArgument('temporary') | ||||
| ->setHelp( | ->setHelp( | ||||
| pht( | pht( | ||||
| 'Mark the file as temporary. Temporary files will be '. | 'Mark the file as temporary. Temporary files will be '. | ||||
| 'deleted after 24 hours.')), | 'deleted after 24 hours.')), | ||||
| $this->newWorkflowArgument('paths') | $this->newWorkflowArgument('paths') | ||||
| ->setWildcard(true) | ->setWildcard(true) | ||||
| ->setIsPathArgument(true), | ->setIsPathArgument(true), | ||||
| ); | ); | ||||
| } | } | ||||
| public function runWorkflow() { | public function runWorkflow() { | ||||
| if (!$this->getArgument('paths')) { | if (!$this->getArgument('paths')) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht('Specify one or more paths to files you want to upload.')); | pht('Specify one or more paths to files you want to upload.')); | ||||
| } | } | ||||
| $is_temporary = $this->getArgument('temporary'); | $is_temporary = $this->getArgument('temporary'); | ||||
| $is_json = $this->getArgument('json'); | $is_json = $this->getArgument('json'); | ||||
| $is_browse = $this->getArgument('browse'); | |||||
| $paths = $this->getArgument('paths'); | $paths = $this->getArgument('paths'); | ||||
| $conduit = $this->getConduitEngine(); | $conduit = $this->getConduitEngine(); | ||||
| $results = array(); | $results = array(); | ||||
| $uploader = id(new ArcanistFileUploader()) | $uploader = id(new ArcanistFileUploader()) | ||||
| ->setConduitEngine($conduit); | ->setConduitEngine($conduit); | ||||
| foreach ($paths as $key => $path) { | foreach ($paths as $key => $path) { | ||||
| $file = id(new ArcanistFileDataRef()) | $file = id(new ArcanistFileDataRef()) | ||||
| ->setName(basename($path)) | ->setName(basename($path)) | ||||
| ->setPath($path); | ->setPath($path); | ||||
| if ($is_temporary) { | if ($is_temporary) { | ||||
| $expires_at = time() + phutil_units('24 hours in seconds'); | $expires_at = time() + phutil_units('24 hours in seconds'); | ||||
| $file->setDeleteAfterEpoch($expires_at); | $file->setDeleteAfterEpoch($expires_at); | ||||
| } | } | ||||
| $uploader->addFile($file); | $uploader->addFile($file); | ||||
| } | } | ||||
| $files = $uploader->uploadFiles(); | $files = $uploader->uploadFiles(); | ||||
| $results = array(); | $phids = array(); | ||||
| foreach ($files as $file) { | foreach ($files as $file) { | ||||
| // TODO: This could be handled more gracefully; just preserving behavior | // TODO: This could be handled more gracefully. | ||||
| // until we introduce `file.query` and modernize this. | |||||
| if ($file->getErrors()) { | if ($file->getErrors()) { | ||||
| throw new Exception(implode("\n", $file->getErrors())); | throw new Exception(implode("\n", $file->getErrors())); | ||||
| } | } | ||||
| $phid = $file->getPHID(); | $phids[] = $file->getPHID(); | ||||
| $name = $file->getName(); | } | ||||
| $info = $conduit->resolveCall( | |||||
| 'file.info', | |||||
| array( | |||||
| 'phid' => $phid, | |||||
| )); | |||||
| $results[$path] = $info; | $symbols = $this->getSymbolEngine(); | ||||
| $symbol_refs = $symbols->loadFilesForSymbols($phids); | |||||
| if (!$is_json) { | $refs = array(); | ||||
| $id = $info['id']; | foreach ($symbol_refs as $symbol_ref) { | ||||
| echo " F{$id} {$name}: ".$info['uri']."\n\n"; | $ref = $symbol_ref->getObject(); | ||||
| if ($ref === null) { | |||||
| throw new Exception( | |||||
| pht( | |||||
| 'Failed to resolve symbol ref "%s".', | |||||
| $symbol_ref->getSymbol())); | |||||
| } | } | ||||
| $refs[] = $ref; | |||||
| } | } | ||||
| if ($is_json) { | if ($is_json) { | ||||
| $output = id(new PhutilJSON())->encodeFormatted($results); | $json = array(); | ||||
| echo $output; | |||||
| foreach ($refs as $key => $ref) { | |||||
| $uri = $ref->getURI(); | |||||
| $uri = $this->getAbsoluteURI($uri); | |||||
| $map = array( | |||||
| 'argument' => $paths[$key], | |||||
| 'id' => $ref->getID(), | |||||
| 'phid' => $ref->getPHID(), | |||||
| 'name' => $ref->getName(), | |||||
| 'uri' => $uri, | |||||
| ); | |||||
| $json[] = $map; | |||||
| } | |||||
| echo id(new PhutilJSON())->encodeAsList($json); | |||||
| } else { | } else { | ||||
| $this->writeStatus(pht('Done.')); | foreach ($refs as $ref) { | ||||
| $uri = $ref->getURI(); | |||||
| $uri = $this->getAbsoluteURI($uri); | |||||
| echo tsprintf( | |||||
| '%s', | |||||
| $ref->newDisplayRef() | |||||
| ->setURI($uri)); | |||||
| } | |||||
| } | |||||
| if ($is_browse) { | |||||
| $uris = array(); | |||||
| foreach ($refs as $ref) { | |||||
| $uri = $ref->getURI(); | |||||
| $uri = $this->getAbsoluteURI($uri); | |||||
| $uris[] = $uri; | |||||
| } | |||||
| $this->openURIsInBrowser($uris); | |||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| private function writeStatus($line) { | private function writeStatus($line) { | ||||
| $this->writeStatusMessage($line."\n"); | $this->writeStatusMessage($line."\n"); | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 81 Lines • Show Last 20 Lines | |||||