Skip to content

Commit 1fa8ab0

Browse files
committed
refactor: change file structure
1 parent e050e54 commit 1fa8ab0

File tree

2 files changed

+76
-76
lines changed

2 files changed

+76
-76
lines changed

src/extension.ts

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,14 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from 'vscode';
4-
import * as path from 'path';
4+
import { generatePhpSkeleton } from './genetarePhpSkeletons';
55

66
// This method is called when your extension is activated
77
// Your extension is activated the very first time the command is executed
88
export function activate(context: vscode.ExtensionContext) {
99
console.log('Congratulations, your extension "php-class-generator" is now active!');
10-
context.subscriptions.push(vscode.commands.registerCommand('php-class-generator.generate-php-class', generateFilePhpClass));
10+
context.subscriptions.push(vscode.commands.registerCommand('php-class-generator.generate-php-class', generatePhpSkeleton));
1111
}
1212

1313
// This method is called when your extension is deactivated
14-
export function deactivate() { }
15-
16-
17-
async function generateFilePhpClass() {
18-
19-
let className = await vscode.window.showInputBox({
20-
placeHolder: 'Name of class'
21-
});
22-
23-
if (!className) {
24-
vscode.window.showInformationMessage("It is required to provide a name for the class");
25-
return;
26-
}
27-
28-
className = capitalizeAndTrim(className);
29-
const classSkeleton = generateSkeleton(className);
30-
31-
const pathFile = getPathFile(className);
32-
if (!pathFile) {
33-
vscode.window.showInformationMessage("Working folder not found, open a folder an try again");
34-
return;
35-
}
36-
37-
// const rootPath = vscode.workspace.rootPath;
38-
// let pathFile = vscode.Uri.parse(`file:` + path.join(`${rootPath}`, `${className}.php`));
39-
40-
await vscode.workspace.fs.writeFile(pathFile, Buffer.from(classSkeleton));
41-
vscode.window.showTextDocument(pathFile);
42-
}
43-
44-
45-
function generateSkeleton(className: string): string {
46-
return `<?php
47-
48-
class ${className}
49-
{
50-
public function __construct()
51-
{
52-
}
53-
}`;
54-
55-
}
56-
57-
function getPathFile(className: string): vscode.Uri | null {
58-
59-
let fsPath = null;
60-
61-
// Create in current file directory
62-
if (vscode.window.activeTextEditor) {
63-
const currentlyOpenTabfileUri = vscode.window.activeTextEditor.document.uri;
64-
const currentlyOpenTabfileName = path.basename(currentlyOpenTabfileUri.toString());
65-
fsPath = currentlyOpenTabfileUri.fsPath.replace(currentlyOpenTabfileName, "");
66-
}
67-
68-
// Create in root workspace
69-
if (!fsPath && vscode.workspace.workspaceFolders !== undefined) {
70-
fsPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
71-
}
72-
73-
if (fsPath) {
74-
return vscode.Uri.parse(`file:` + path.join(`${fsPath}`, `${className}.php`));
75-
}
76-
77-
return null;
78-
}
79-
80-
function capitalizeAndTrim(str: string): string {
81-
const words = str.split(' ');
82-
let result = "";
83-
for (let word of words) {
84-
result += word.charAt(0).toUpperCase() + word.slice(1);
85-
}
86-
return result;
87-
}
14+
export function deactivate() { }

src/genetarePhpSkeletons.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as vscode from 'vscode';
2+
import * as path from 'path';
3+
4+
export async function generatePhpSkeleton() {
5+
6+
let className = await vscode.window.showInputBox({
7+
placeHolder: 'Name of class'
8+
});
9+
10+
if (!className) {
11+
vscode.window.showInformationMessage("It is required to provide a name for the class");
12+
return;
13+
}
14+
15+
className = capitalizeAndTrim(className);
16+
const classSkeleton = generatePhpClassSkeleton(className);
17+
18+
const pathFile = getPathFile(className);
19+
if (!pathFile) {
20+
vscode.window.showInformationMessage("Working folder not found, open a folder an try again");
21+
return;
22+
}
23+
24+
await vscode.workspace.fs.writeFile(pathFile, Buffer.from(classSkeleton));
25+
vscode.window.showTextDocument(pathFile);
26+
}
27+
28+
29+
function generatePhpClassSkeleton(className: string): string {
30+
return `<?php
31+
32+
## TODO: generate namespace
33+
34+
class ${className}
35+
{
36+
public function __construct()
37+
{
38+
}
39+
}`;
40+
41+
}
42+
43+
function getPathFile(className: string): vscode.Uri | null {
44+
45+
let fsPath = null;
46+
47+
// Create in current file directory
48+
if (vscode.window.activeTextEditor) {
49+
const currentlyOpenTabfileUri = vscode.window.activeTextEditor.document.uri;
50+
const currentlyOpenTabfileName = path.basename(currentlyOpenTabfileUri.toString());
51+
fsPath = currentlyOpenTabfileUri.fsPath.replace(currentlyOpenTabfileName, "");
52+
}
53+
54+
// Create in root workspace
55+
if (!fsPath && vscode.workspace.workspaceFolders !== undefined) {
56+
fsPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
57+
}
58+
59+
if (fsPath) {
60+
return vscode.Uri.parse(`file:` + path.join(`${fsPath}`, `${className}.php`));
61+
}
62+
63+
return null;
64+
}
65+
66+
function capitalizeAndTrim(str: string): string {
67+
const words = str.split(' ');
68+
let result = "";
69+
for (let word of words) {
70+
result += word.charAt(0).toUpperCase() + word.slice(1);
71+
}
72+
return result;
73+
}

0 commit comments

Comments
 (0)