Skip to content

Commit

Permalink
Add HelmProcessor to handle helm references (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalre committed Jan 18, 2023
1 parent 1d72e20 commit 176264e
Show file tree
Hide file tree
Showing 9 changed files with 337 additions and 120 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/adapter/vs-code-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ export class VsCodeAdapter {
if (severity === Severity.ERROR) {
window.showErrorMessage(message)
} else {
if (this.settings.getNotifySuccess()) {
window.showInformationMessage(message)
}
this.notify(message)
}
}

notify(message: string) {
if (this.settings.getNotifySuccess()) {
window.showInformationMessage(message)
}
}

Expand Down
19 changes: 6 additions & 13 deletions src/util/comment-util.ts → src/processor/comment-processor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { JsYamlAdapter } from "../adapter/js-yaml-adapter"
import { Settings } from "../settings"

export class CommentUtil {
settings: Settings
jsyamladapter: JsYamlAdapter
export class CommentProcessor {
comments: string[][] = []
text: string
lines: string[]

constructor(text: string, settings = new Settings()) {
constructor(text: string) {
this.text = text.trim()
this.lines = this.text.split("\n")
this.settings = settings
this.jsyamladapter = new JsYamlAdapter(settings)
}

/**
Expand All @@ -21,7 +14,7 @@ export class CommentUtil {
*/
findComments() {
this.lines.forEach((line, index) => {
if (CommentUtil.isLineComment(line)) {
if (CommentProcessor.isLineComment(line)) {
this.addLineToComments(index)
}
})
Expand Down Expand Up @@ -66,7 +59,7 @@ export class CommentUtil {

insert(comment: string[]) {
const indexOfComment = this.getIndexOfString(comment[1])
if (CommentUtil.isCommentFound(indexOfComment)) {
if (CommentProcessor.isCommentFound(indexOfComment)) {
const textAfter = this.text.slice(indexOfComment)
const textBefore = this.text.slice(0, indexOfComment)
this.text = `${textBefore}${comment[0]}\n${textAfter.trimEnd()}`
Expand All @@ -83,12 +76,12 @@ export class CommentUtil {

search(text: string) {
let result = this.searchExactMatch(text)
if (CommentUtil.isCommentFound(result)) {
if (CommentProcessor.isCommentFound(result)) {
return result
}

result = this.searchFuzzyForTrimmedText(text)
if (CommentUtil.isCommentFound(result)) {
if (CommentProcessor.isCommentFound(result)) {
return result
}

Expand Down
38 changes: 38 additions & 0 deletions src/processor/helm-processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export class HelmProcessor {
store: Map<string, string> = new Map()
text: string

constructor(text: string) {
this.text = text
}

preprocess() {
const helmValues = this.findHelmValues()
if (helmValues) {
for (const value of helmValues) {
this.replaceValueWithSubstitue(value)
}
}
}

findHelmValues() {
return this.text.match(/{{.*}}/g)
}

replaceValueWithSubstitue(value: string) {
const substitue = `vscode-yaml-sort.helm.${this.store.size}`
this.store.set(substitue, value)
this.text = this.text.replace(value, substitue)
}

postprocess() {
this.store.forEach((value: string, key: string) => {
this.replaceSubstituteWithValue(key, value)
})
}

replaceSubstituteWithValue(substitue: string, value: string) {
const match = new RegExp("('|\")?" + substitue + "('|\")?")
this.text = this.text.replace(match, value)
}
}
31 changes: 31 additions & 0 deletions src/processor/processor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CommentProcessor } from "./comment-processor"
import { HelmProcessor } from "./helm-processor"

export class Processor {
text: string
helmprocessor!: HelmProcessor
commentprocessor!: CommentProcessor

constructor(text: string) {
this.text = text
}

preprocess() {
this.helmprocessor = new HelmProcessor(this.text)
this.helmprocessor.preprocess()
this.text = this.helmprocessor.text

this.commentprocessor = new CommentProcessor(this.text)
this.commentprocessor.findComments()
this.text = this.commentprocessor.text
}

postprocess(text: string) {
this.commentprocessor.applyComments(text)
this.text = this.commentprocessor.text

this.helmprocessor.text = this.text
this.helmprocessor.postprocess()
this.text = this.helmprocessor.text
}
}
Loading

0 comments on commit 176264e

Please sign in to comment.