Skip to content

Commit 79b8b72

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

File tree

5 files changed

+120
-75
lines changed

5 files changed

+120
-75
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ With this extension, you can quickly generate a skeleton for a PHP class just by
99
## Planned Features
1010

1111
- Generate PSR-4 namespace
12+
- Generate skeleton of other types of PHP files [interface, trait, enum, value object]
1213

1314
## Requirements
1415

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
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 { generatePhpSkeleton } from './genetarePhpSkeletons';
4+
import { wizardGeneratePhpSkeleton } from './wizardPhpSkeletons';
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', generatePhpSkeleton));
10+
context.subscriptions.push(vscode.commands.registerCommand('php-class-generator.generate-php-class', wizardGeneratePhpSkeleton));
1111
}
1212

1313
// This method is called when your extension is deactivated

src/genetarePhpSkeletons.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/utils/files.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as vscode from 'vscode';
2+
import * as path from 'path';
3+
4+
export function getPathFile(className: string): vscode.Uri
5+
{
6+
7+
let fsPath = null;
8+
9+
// Create in current file directory
10+
if (vscode.window.activeTextEditor) {
11+
const currentlyOpenTabfileUri = vscode.window.activeTextEditor.document.uri;
12+
const currentlyOpenTabfileName = path.basename(currentlyOpenTabfileUri.toString());
13+
fsPath = currentlyOpenTabfileUri.fsPath.replace(currentlyOpenTabfileName, "");
14+
}
15+
16+
// Create in root workspace
17+
if (!fsPath && vscode.workspace.workspaceFolders !== undefined) {
18+
fsPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
19+
}
20+
21+
if (!fsPath) {
22+
throw new Error('Working folder not found, open a folder an try again');
23+
}
24+
25+
return vscode.Uri.parse(`file:` + path.join(`${fsPath}`, `${className}.php`));
26+
}
27+
28+
export async function writeFile(pathFile: vscode.Uri, skeleton: string): Promise<void>
29+
{
30+
await vscode.workspace.fs.writeFile(pathFile, Buffer.from(skeleton));
31+
}
32+
33+
export function showFile(pathFile: vscode.Uri): void
34+
{
35+
vscode.window.showTextDocument(pathFile);
36+
}

src/wizardPhpSkeletons.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import * as vscode from 'vscode';
2+
import { getPathFile, showFile, writeFile } from './utils/files';
3+
4+
export async function wizardGeneratePhpSkeleton() {
5+
6+
const type = await wizardFileType();
7+
const fileName = await wizardFileName(type);
8+
const classSkeleton = generatePhpSkeleton(type,fileName);
9+
10+
const pathFile = getPathFile(fileName);
11+
12+
await writeFile(pathFile, classSkeleton);
13+
14+
showFile(pathFile);
15+
}
16+
17+
async function wizardFileType(): Promise<string>
18+
{
19+
const type = await vscode.window.showQuickPick(
20+
[
21+
"class",
22+
// "interface",
23+
// "trait",
24+
// "enum",
25+
// "value object (immutable class)"
26+
],
27+
{
28+
placeHolder: "select the type of file you want to create"
29+
}
30+
);
31+
32+
if (!type) {
33+
throw new Error('It is required to provide a type for the file');
34+
}
35+
return type;
36+
}
37+
38+
async function wizardFileName(type: string): Promise<string>
39+
{
40+
let className = await vscode.window.showInputBox({
41+
placeHolder: `Name of ${type}`
42+
});
43+
if (!className) {
44+
throw new Error("It is required to provide a name for the class");
45+
}
46+
className = capitalizeAndTrim(className);
47+
return className;
48+
}
49+
50+
function generatePhpSkeleton(type: string,className: string): string
51+
{
52+
if(type === "class"){
53+
return generatePhpClassSkeleton(className);
54+
}
55+
return "## TODO";
56+
}
57+
58+
function generatePhpClassSkeleton(className: string): string {
59+
return `<?php
60+
61+
## TODO: generate namespace
62+
63+
class ${className}
64+
{
65+
public function __construct()
66+
{
67+
}
68+
}`;
69+
70+
}
71+
72+
73+
74+
function capitalizeAndTrim(str: string): string {
75+
const words = str.split(' ');
76+
let result = "";
77+
for (let word of words) {
78+
result += word.charAt(0).toUpperCase() + word.slice(1);
79+
}
80+
return result;
81+
}

0 commit comments

Comments
 (0)