From a6c0b5884ac785e430250a193b3907ceea5c74f2 Mon Sep 17 00:00:00 2001 From: garrettmills Date: Thu, 5 Mar 2020 12:15:00 -0600 Subject: [PATCH] Add git.tag state; abstract out AbstractGitState class --- app/classes/git/Repository.js | 12 ++++++++- app/classes/state/git/AbstractGitState.js | 14 ++++++++++ app/classes/state/git/CheckoutState.js | 11 ++------ app/classes/state/git/CloneState.js | 11 ++------ app/classes/state/git/TagState.js | 31 +++++++++++++++++++++++ app/services/states.service.js | 1 + 6 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 app/classes/state/git/AbstractGitState.js create mode 100644 app/classes/state/git/TagState.js diff --git a/app/classes/git/Repository.js b/app/classes/git/Repository.js index cd1795f..c86f046 100644 --- a/app/classes/git/Repository.js +++ b/app/classes/git/Repository.js @@ -50,10 +50,20 @@ class Repository extends Injectable { await this._git_cmd(`commit -m "${message}"`) } - async tag(label) { + async create_tag(label) { await this._git_cmd(`tag "${label}"`) } + async delete_tag(label) { + await this._git_cmd(`tag --delete "${label}"`) + } + + async get_tag(label) { + const cd_cmd = await this._host.get_directory_change_command(this._path) + const cmd = `${cd_cmd} && git rev-parse "${label}"` + return await this._host.run_line_result(cmd) + } + async push(target = false, ref = false) { await this._git_cmd(`push${target ? ' '+target : ''}${ref ? ' '+ref : ''} --tags`) } diff --git a/app/classes/state/git/AbstractGitState.js b/app/classes/state/git/AbstractGitState.js new file mode 100644 index 0000000..eef228e --- /dev/null +++ b/app/classes/state/git/AbstractGitState.js @@ -0,0 +1,14 @@ +const State = require('../State') +const Repository = require('../../git/Repository') + +class AbstractGitState extends State { + + async _repo() { + const path = await this._host.get_path(this._config.path) + if ( !path.is_valid() ) await path.touch(true) + return new Repository(path) + } + +} + +module.exports = exports = AbstractGitState diff --git a/app/classes/state/git/CheckoutState.js b/app/classes/state/git/CheckoutState.js index 7f3086d..d25937a 100644 --- a/app/classes/state/git/CheckoutState.js +++ b/app/classes/state/git/CheckoutState.js @@ -1,7 +1,6 @@ -const State = require('../State') -const Repository = require('../../git/Repository') +const AbstractGitState = require('./AbstractGitState') -class CheckoutState extends State { +class CheckoutState extends AbstractGitState { static get services() { return [...super.services, 'output'] } @@ -34,12 +33,6 @@ class CheckoutState extends State { } } - async _repo() { - const path = await this._host.get_path(this._config.path) - if ( !path.is_valid() ) await path.touch(true) - return new Repository(path) - } - } module.exports = exports = CheckoutState diff --git a/app/classes/state/git/CloneState.js b/app/classes/state/git/CloneState.js index 8bfcdeb..7d3c814 100644 --- a/app/classes/state/git/CloneState.js +++ b/app/classes/state/git/CloneState.js @@ -1,7 +1,6 @@ -const State = require('../State') -const Repository = require('../../git/Repository') +const AbstractGitState = require('./AbstractGitState') -class CloneState extends State { +class CloneState extends AbstractGitState { async apply() { if ( !(await this.check()) ) { @@ -22,12 +21,6 @@ class CloneState extends State { } } - async _repo() { - const path = await this._host.get_path(this._config.path) - if ( !path.is_valid() ) await path.touch(true) - return new Repository(path) - } - } module.exports = exports = CloneState diff --git a/app/classes/state/git/TagState.js b/app/classes/state/git/TagState.js new file mode 100644 index 0000000..36a78ba --- /dev/null +++ b/app/classes/state/git/TagState.js @@ -0,0 +1,31 @@ +const AbstractGitState = require('./AbstractGitState') + +class TagState extends AbstractGitState { + + async apply() { + if ( !(await this.check()) ) { + const repo = await this._repo() + await repo.create_tag(this._config.tag) + } + } + + async check() { + const repo = await this._repo() + try { + await repo.get_tag(this._config.tag) + return true + } catch (e) { + return false + } + } + + async reverse() { + if ( await this.check() ) { + const repo = await this._repo() + await repo.delete_tag(this._config.tag) + } + } + +} + +module.exports = exports = TagState diff --git a/app/services/states.service.js b/app/services/states.service.js index 86500a7..4b44fa4 100644 --- a/app/services/states.service.js +++ b/app/services/states.service.js @@ -25,6 +25,7 @@ class StatesService extends Service { 'git.clone': require('../classes/state/git/CloneState'), 'git.checkout': require('../classes/state/git/CheckoutState'), + 'git.tag': require('../classes/state/git/TagState'), 'os.cmd': require('../classes/state/os/CommandState'),