Skip to content

Commit

Permalink
'3.1demo'
Browse files Browse the repository at this point in the history
  • Loading branch information
ElouanDai committed Apr 28, 2024
1 parent 634022e commit 14c51d7
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/1_basic_usage/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { SceneConfig, Scene, ArkFile, ArkNamespace, ArkClass, ArkField, ArkMethod, Cfg } from "./bundle";

const config_path = "./target_project/project_config.json";
let config: SceneConfig = new SceneConfig();
config.buildFromJson(config_path);

let scene: Scene = new Scene(config);

//ex01.1:获取所有文件
let files: ArkFile[] = scene.getFiles();
let fileNames: string[] = files.map(file => file.name);
console.log(fileNames);

//ex01.2:获取命名空间
let namespaces: ArkNamespace[] = scene.getAllNamespacesUnderTargetProject();
let namespaceNames: string[] = namespaces.map(ns => ns.name);
console.log(namespaceNames);

let namespaces2: ArkNamespace[] = files[1].getNamespaces();
let namespaceNames2: string[] = namespaces2.map(ns => ns.name);
console.log(namespaceNames2);

//ex01.3:获取所有类
let classes: ArkClass[] = scene.getAllClassesUnderTargetProject();
let classNames: string[] = classes.map(cls => cls.name);
console.log(classNames);

let classes2: ArkClass[] = files[2].getClasses();
let classNames2: string[] = classes2.map(cls => cls.name);
console.log(classNames2);

let classes3: ArkClass[] = namespaces[0].getClasses();
let classNames3: string[] = classes3.map(cls => cls.name);
console.log(classNames3);

//ex01.4:获取所有属性
let bookClass: ArkClass = classes[3];
let fields: ArkField[] = bookClass.getFields();
let fieldNames: string[] = fields.map(fld => fld.name);
console.log(fieldNames);

//ex01.5:获取所有方法
let serviceClass: ArkClass = classes[5];
let methods: ArkMethod[] = serviceClass.getMethods();
let methodNames: string[] = methods.map(mthd => mthd.name);
console.log(methodNames);

let methods1: ArkMethod[] = scene.getAllMethodsUnderTargetProject();
let methods2: ArkMethod[] = files[2].getAllMethodsUnderThisFile();

let methodNames1: string[] = methods1.map(mthd => mthd.name);
let methodNames2: string[] = methods2.map(mthd => mthd.name);

console.log(methodNames1);
console.log(methodNames2);

//ex01.6:获取方法CFG
let addBookMethod: ArkMethod = methods[0];
let addBookCfg: Cfg = addBookMethod.getBody().getCfg();

//ex01.7:三地址码打印并观察语法糖变化



console.log("finish")
10 changes: 10 additions & 0 deletions src/1_basic_usage/target_project/project_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"targetProjectName": "target_project",
"targetProjectDirectory": "target_project",

"logPath": "out\\log.txt",
"ohosSdkPath": "",
"kitSdkPath": "",
"systemSdkPath": "",
"otherSdks": []
}
7 changes: 7 additions & 0 deletions src/1_basic_usage/target_project/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Library } from "./models/book";
import { BookService } from "./services/bookService";

const bookService = new BookService();
bookService.addBook(new Library.Book("The Hobbit", "J.R.R. Tolkien"));
const books = bookService.getAllBooks();
console.log(books);
10 changes: 10 additions & 0 deletions src/1_basic_usage/target_project/src/models/book.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export namespace Library {
export class Book {
public title: string;
public author: string;
constructor(title: string,author: string) {
this.title = title;
this.author = author;
}
}
}
13 changes: 13 additions & 0 deletions src/1_basic_usage/target_project/src/services/bookService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Library } from "../models/book";

export class BookService {
private books: Library.Book[] = [];

public addBook(book: Library.Book): void {
this.books.push(book);
}

public getAllBooks(): Library.Book[] {
return this.books;
}
}

0 comments on commit 14c51d7

Please sign in to comment.