Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/global #56

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"docx"
],
"activationEvents": [
"workspaceContains:.docx.json"
"onStartupFinished"
],
"main": "./out/extension.js",
"contributes": {
Expand Down
4 changes: 3 additions & 1 deletion src/api/repository.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LocalProvider } from '../provider/local.provider'
import { FileSystemManager } from '../utils/fileSystem.utils'
import { ProviderConfig } from './repository.controller'
import { WebProvider } from '../provider/web.provider'
import { WorkspaceManager } from '../utils/workspace.utils'

export type Provider = LocalProvider | GithubProvider | GitlabProvider | WebProvider

Expand All @@ -15,7 +16,8 @@ export class RepositoryProvider {
switch (config.type) {
case 'local':
{
const fileSystem = new FileSystemManager(workspace.fs, config.ignorePatterns)
const workspaceFolder = WorkspaceManager.getWorkspaceFolder()
const fileSystem = new FileSystemManager(workspace.fs, `${workspaceFolder}/.gitignore`)
this.provider = new LocalProvider(fileSystem)
}
break
Expand Down
9 changes: 8 additions & 1 deletion src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ import { GenerateDocxJsonCommand } from './config.generate.command'
import { CleanupDocxJsonCommand } from './config.cleanup.command'
import { GithubTokenCommand } from './github.token.command'
import { GitlabTokenCommand } from './gitlab.token.command'
import { DropdownCommand } from './dropdown.command'

export { GenerateDocxJsonCommand, CleanupDocxJsonCommand, GithubTokenCommand, GitlabTokenCommand }
export {
GenerateDocxJsonCommand,
CleanupDocxJsonCommand,
GithubTokenCommand,
GitlabTokenCommand,
DropdownCommand,
}
10 changes: 6 additions & 4 deletions src/config/config.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export class ConfigGenerator {
public async generateDocxJson(rootPath: string, configFilePath: string) {
try {
const existingConfig = await this.readDocxJson(configFilePath)
const folderObject = await this.createFolderObject(rootPath, existingConfig.ignorePatterns)
const mergedIgnorePatterns = Array.from(
new Set([...this.fileSystem.ignorePatterns, ...(existingConfig.ignorePatterns ?? [])])
)
const folderObject = await this.createFolderObject(rootPath, mergedIgnorePatterns)
const newConfig = this.mergeConfigurations(existingConfig, folderObject)
await this.writeDocxJson(newConfig, configFilePath)
} catch (error) {
Expand All @@ -33,7 +36,7 @@ export class ConfigGenerator {

private async createFolderObject(
directoryPath: string,
ignorePatterns: string[] = [],
ignorePatterns: string[],
parentPath = ''
): Promise<Record<string, string[]>> {
const entries = await this.fileSystem.retrieveNonIgnoredEntries(directoryPath)
Expand Down Expand Up @@ -65,8 +68,7 @@ export class ConfigGenerator {
try {
const fileContent = await this.fileSystem.readFile(filePath)
return this.fileSystem.processFileContent<DocAssociationsConfig>(fileContent)
} catch (error) {
ErrorManager.outputError(`An error occur when trying to read the config file. ${error}`)
} catch {
return { ignorePatterns: [], associations: {} }
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/extension.manager.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { ExtensionContext, commands } from 'vscode'
import { ExtensionContext, commands, workspace } from 'vscode'
import { ConfigGenerator } from './config/config.manager'
import { FileSystemManager } from './utils/fileSystem.utils'
import {
CleanupDocxJsonCommand,
GenerateDocxJsonCommand,
GithubTokenCommand,
GitlabTokenCommand,
DropdownCommand,
} from './commands'
import { CommandRegistry } from './commands/command.registry'
import { Notifier, VsCodeNotifier } from './utils/notifier.utils'
import { DropdownCommand } from './commands/dropdown.command'
import { Documentation } from './association.manager'
import { WorkspaceManager } from './utils/workspace.utils'

export class ExtensionManager {
private commandRegistry: CommandRegistry
Expand All @@ -20,11 +21,12 @@ export class ExtensionManager {
private fileSystem: FileSystemManager
private documentations: Documentation[]
private jsonConfig: string
private workspaceFolder = WorkspaceManager.getWorkspaceFolder()

constructor(context: ExtensionContext, documentations: Documentation[], jsonConfig: string) {
this.commandRegistry = new CommandRegistry()
this.notifier = new VsCodeNotifier()
this.fileSystem = new FileSystemManager()
this.fileSystem = new FileSystemManager(workspace.fs, `${this.workspaceFolder}/.gitignore`)
this.generator = new ConfigGenerator(this.fileSystem)
this.context = context
this.documentations = documentations
Expand Down
20 changes: 12 additions & 8 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ export async function activate(context: vscode.ExtensionContext) {
)

const refreshDocumentations = async (): Promise<[string, Documentation[]]> => {
const jsonConfig = await fileSystem.readFile(`${workspaceFolder}/${configFilename}`)
const repositoryController = await RepositoryController.create(
jsonConfig,
providerStrategies,
tokens
)
const documentations = await repositoryController.getDocumentations()
return [jsonConfig, documentations]
try {
const jsonConfig = await fileSystem.readFile(`${workspaceFolder}/${configFilename}`)
const repositoryController = await RepositoryController.create(
jsonConfig,
providerStrategies,
tokens
)
const documentations = await repositoryController.getDocumentations()
return [jsonConfig, documentations]
} catch (error) {
return ['', []]
}
}

let tokens = await credentialManager.getTokens()
Expand Down
37 changes: 32 additions & 5 deletions src/utils/fileSystem.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const extensionsOfInterest = ['.md', '.bpmn', '.html'] as const
export type Extension = (typeof extensionsOfInterest)[number]

export class FileSystemManager {
private ignorePatterns: string[] = []
public ignorePatterns: string[] = ['.git']

constructor(
private fs: FileSystem = workspace.fs,
ignorePatterns?: string[]
gitignoreFilePath?: string
) {
this.fs = fs
if (ignorePatterns && ignorePatterns.length) this.ignorePatterns = ignorePatterns
if (gitignoreFilePath) this.loadGitignorePatterns(gitignoreFilePath)
}

public async ensureFileExists(uri: Uri): Promise<boolean> {
Expand Down Expand Up @@ -93,8 +93,35 @@ export class FileSystemManager {
}

private isEntryIgnored(fullPath: string, entryName: string): boolean {
return this.ignorePatterns.some(
(pattern) => minimatch(entryName, pattern) || minimatch(fullPath, pattern)
return this.ignorePatterns.some((pattern) =>
this.isPatternMatching(pattern, fullPath, entryName)
)
}

private isPatternMatching(pattern: string, fullPath: string, entryName: string): boolean {
const isRootLevel = pattern.startsWith('/')
const normalizedPattern = this.normalizePattern(pattern)
const matchFullPath = isRootLevel
? fullPath.startsWith(normalizedPattern)
: fullPath.includes(normalizedPattern)
const matchEntryName =
minimatch(entryName, normalizedPattern) || minimatch(fullPath, normalizedPattern)
return matchEntryName || matchFullPath
}

private normalizePattern(pattern: string): string {
return pattern.replace(/^\//, '') // Remove leading slash
}

private async loadGitignorePatterns(gitignoreFilePath: string) {
try {
const fileContent = await this.readFile(gitignoreFilePath)
const gitignorePatterns = fileContent
.split('\n')
.filter((pattern) => pattern && !pattern.startsWith('#'))
this.ignorePatterns = [...this.ignorePatterns, ...gitignorePatterns]
} catch (error) {
/* empty */
}
}
}
Loading