Skip to content

Commit

Permalink
add basic rename command for dvc tracked paths (#792)
Browse files Browse the repository at this point in the history
Co-authored-by: Roger <[email protected]>
  • Loading branch information
mattseddon and rogermparent authored Sep 8, 2021
1 parent 51888e7 commit 60dbaa3
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 17 deletions.
42 changes: 28 additions & 14 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@
"command": "dvc.copyRelativeFilePath",
"category": "DVC"
},
{
"title": "%command.renameTarget%",
"command": "dvc.renameTarget",
"category": "DVC"
},
{
"title": "%command.addExperimentsTableSort%",
"command": "dvc.addExperimentsTableSort",
Expand Down Expand Up @@ -383,6 +388,10 @@
"command": "dvc.copyRelativeFilePath",
"when": "false"
},
{
"command": "dvc.renameTarget",
"when": "false"
},
{
"command": "dvc.views.experimentsFilterByTree.removeAllFilters",
"when": "false"
Expand Down Expand Up @@ -489,23 +498,13 @@
],
"view/item/context": [
{
"command": "dvc.deleteTarget",
"group": "DVC",
"when": "viewItem =~ /^dvcTracked/"
},
{
"command": "dvc.removeTarget",
"when": "viewItem == dvcTrackedData",
"group": "DVC"
},
{
"command": "dvc.pushTarget",
"group": "DVC",
"command": "dvc.pullTarget",
"group": "1_DVC@1",
"when": "viewItem == dvcTrackedData || viewItem == dvcTrackedHasRemote"
},
{
"command": "dvc.pullTarget",
"group": "DVC",
"command": "dvc.pushTarget",
"group": "1_DVC@2",
"when": "viewItem == dvcTrackedData || viewItem == dvcTrackedHasRemote"
},
{
Expand All @@ -518,6 +517,21 @@
"group": "6_copypath@2",
"when": "viewItem =~ /^dvcTracked/"
},
{
"command": "dvc.renameTarget",
"group": "7_modification@1",
"when": "viewItem == dvcTrackedData"
},
{
"command": "dvc.deleteTarget",
"group": "7_modification@2",
"when": "viewItem =~ /^dvcTracked/"
},
{
"command": "dvc.removeTarget",
"when": "viewItem == dvcTrackedData",
"group": "7_modification@2"
},
{
"command": "dvc.addExperimentsTableSort",
"group": "inline",
Expand Down
1 change: 1 addition & 0 deletions extension/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"command.push": "Push",
"command.pushTarget": "Push",
"command.removeTarget": "Remove",
"command.renameTarget": "Rename",
"command.views.experimentsFilterByTree.removeFilter": "Remove Filter From Experiments Table",
"command.views.experimentsFilterByTree.removeAllFilters": "Remove All Filters From Experiments Table",
"command.views.experimentsSortByTree.removeSort": "Remove Sort From Experiments Table",
Expand Down
1 change: 1 addition & 0 deletions extension/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export enum Command {
EXPERIMENT = 'exp',
INITIALIZE = 'init',
LIST = 'list',
MOVE = 'move',
PULL = 'pull',
PUSH = 'push',
REMOVE = 'remove',
Expand Down
24 changes: 24 additions & 0 deletions extension/src/cli/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,30 @@ describe('CliExecutor', () => {
})
})

describe('move', () => {
it('should call createProcess with the correct parameters to move a DVC tracked target', async () => {
const cwd = __dirname
const target = 'data/data.xml.dvc'
const destination = 'data/data1.xml.dvc'
const stdout = `
To track the changes with git, run:
git add ${destination} data/.gitignore`

mockedCreateProcess.mockReturnValueOnce(getMockedProcess(stdout))

const output = await cliExecutor.move(cwd, target, destination)
expect(output).toEqual(stdout)

expect(mockedCreateProcess).toBeCalledWith({
args: ['move', target, destination],
cwd: cwd,
env: mockedEnv,
executable: 'dvc'
})
})
})

describe('pull', () => {
it('should call createProcess with the correct parameters to pull the entire repository', async () => {
const cwd = __dirname
Expand Down
5 changes: 5 additions & 0 deletions extension/src/cli/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const autoRegisteredCommands = {
EXPERIMENT_QUEUE: 'experimentRunQueue',
EXPERIMENT_REMOVE: 'experimentRemove',
INIT: 'init',
MOVE: 'move',
PULL: 'pull',
PUSH: 'push',
REMOVE: 'remove'
Expand Down Expand Up @@ -94,6 +95,10 @@ export class CliExecutor extends Cli {
return this.executeProcess(cwd, Command.INITIALIZE, Flag.SUBDIRECTORY)
}

public move(cwd: string, target: string, destination: string) {
return this.executeProcess(cwd, Command.MOVE, target, destination)
}

public pull(cwd: string, ...args: Args) {
return this.executeProcess(cwd, Command.PULL, ...args)
}
Expand Down
1 change: 0 additions & 1 deletion extension/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export class Cli implements ICli {

public async executeProcess(cwd: string, ...args: Args): Promise<string> {
const { command, ...options } = this.getOptions(cwd, ...args)

const baseEvent: CliEvent = { command, cwd, pid: undefined }
const stopWatch = new StopWatch()
try {
Expand Down
1 change: 1 addition & 0 deletions extension/src/commands/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export enum RegisteredCommands {
PUSH_TARGET = 'dvc.pushTarget',
DELETE_TARGET = 'dvc.deleteTarget',
REMOVE_TARGET = 'dvc.removeTarget',
RENAME_TARGET = 'dvc.renameTarget',

EXTENSION_DESELECT_DEFAULT_PROJECT = 'dvc.deselectDefaultProject',
EXTENSION_SELECT_DEFAULT_PROJECT = 'dvc.selectDefaultProject',
Expand Down
25 changes: 25 additions & 0 deletions extension/src/fileSystem/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '../commands/external'
import { sendViewOpenedTelemetryEvent } from '../telemetry'
import { EventName } from '../telemetry/constants'
import { getInput } from '../vscode/inputBox'

export class TrackedExplorerTree implements TreeDataProvider<string> {
public dispose = Disposable.fn()
Expand Down Expand Up @@ -287,6 +288,30 @@ export class TrackedExplorerTree implements TreeDataProvider<string> {
)
)

this.dispose.track(
registerInstrumentedCommand<string>(
RegisteredCommands.RENAME_TARGET,
async path => {
const dvcRoot = this.pathRoots[path]
const relPath = relative(dvcRoot, path)
const relDestination = await getInput(
'enter a destination relative to the root',
relPath
)
if (!relDestination || relDestination === relPath) {
return
}

return this.internalCommands.executeCommand(
AvailableCommands.MOVE,
dvcRoot,
relPath,
relDestination
)
}
)
)

this.dispose.track(
registerInstrumentedCommand<string>(
RegisteredCommands.PULL_TARGET,
Expand Down
1 change: 1 addition & 0 deletions extension/src/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface IEventNamePropertyMapping {
[EventName.PUSH_TARGET]: undefined
[EventName.PUSH]: undefined
[EventName.REMOVE_TARGET]: undefined
[EventName.RENAME_TARGET]: undefined

[EventName.TRACKED_EXPLORER_OPEN_FILE]: undefined
[EventName.TRACKED_EXPLORER_COPY_FILE_PATH]: undefined
Expand Down
21 changes: 21 additions & 0 deletions extension/src/test/suite/fileSystem/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,27 @@ suite('Tracked Explorer Tree Test Suite', () => {
expect(mockRemove).to.be.calledOnce
})

it('should be able to run dvc.renameTarget without error', async () => {
const relPath = join('mock', 'data', 'MNIST', 'raw')
const absPath = join(dvcDemoPath, relPath)
stub(path, 'relative').returns(relPath)
const mockMove = stub(CliExecutor.prototype, 'move').resolves(
'target moved to new destination'
)

const mockInputBox = stub(window, 'showInputBox').resolves(
relPath + 'est'
)

await commands.executeCommand(RegisteredCommands.RENAME_TARGET, absPath)
expect(mockMove).to.be.calledOnce
expect(mockInputBox).to.be.calledOnce
expect(mockInputBox).to.be.calledWith({
prompt: 'enter a destination relative to the root',
value: relPath
})
})

it('should be able to run dvc.pullTarget without error', async () => {
const relPath = 'data'
const absPath = join(dvcDemoPath, relPath)
Expand Down
5 changes: 3 additions & 2 deletions extension/src/vscode/inputBox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { window } from 'vscode'

export const getInput = (prompt: string) =>
export const getInput = (prompt: string, value?: string) =>
window.showInputBox({
prompt
prompt,
value
})

0 comments on commit 60dbaa3

Please sign in to comment.