KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

プロジェクトと kscript.json

ソースライブラリのインストール

kscript lib add は、公開 GitHub リポジトリから KScript のソースファイルをインストールします。名前だけを指定した場合、KScript は次の対象を検索します。自身の kylepantall/kscript カタログ、orimovo/* と waggr-gb/* に一致する信頼済みの公開リポジトリ、およびプロジェクトに登録されたリポジトリです。各リポジトリでは、まず libraries/<name>/ を確認します。そのフォルダーが存在しない場合は、ルートの kscript.json で、指定された名前のライブラリを宣言するか、それを宣言しているフォルダーを指定できます。

1つだけ一致する場合は、すぐにインストールされます。同じ名前を持つ個別のパッケージが複数ある場合、CLIは一致の数と、修飾された各owner/repo@library名を表示します。いずれか1つを選び、その名前でkscript lib addを再実行してインストールしてください。各owner/repo@libraryは、そのリポジトリが信頼済みであり、かつローカルに登録されている場合でも1回だけ表示されます。ライブラリをGitサブモジュールとして固定する別のカタログには独自の修飾名があり、異なるリビジョンを固定することもできます。

公式ライブラリ 提供する機能 使用するランタイムパッケージ
integer-math 最大公約数、最小公倍数、階乗 コアのみ
app-cache プロジェクトのキャッシュストアと有効期限の設定を使って、計算済みの値をキャッシュします KScript.Cache
request-id 呼び出し元のリクエストIDをそのまま引き継ぐか、レスポンス用に新たに生成するHTTPミドルウェアです。 KScript.Http
isolink-connect Isolink デスクトップセッションを開き、スペースとレコードを操作します。 KScript.Net

Core はすべてのスクリプトで利用できます。表にあるほかのランタイムパッケージは KScript に同梱されており、各ライブラリは使用するパッケージをインポートします。たとえば、Isolink Connect は名前を指定してインストールしてください。

シェル
kscript lib add isolink-connect

新しいプロジェクトでは、信頼された所有者のどちらかの下にある公開リポジトリを明示的に選択できます。例えば、Isolink SDKはwaggr-gb/isolink-connectにあります。

シェル
kscript lib add waggr-gb/isolink-connect@isolink-connect

修飾インストールでは、他の発行元にまたがる探索をスキップします。探索が完了できない場合、または GitHub の API 上限に近い場合に使用してください。ただし、選択したリビジョンを解決するために API の割り当てはまだ必要です。GitHub の割り当てを使い切っている場合は、リセットされてから再試行してください。

インストーラーは、サイトのルートから使用するインポートを表示します。SDK 0.3 では、マニフェストのエントリーclient.kscriptと、KScript.Netからの汎用の@with_loopback_jsonトランスポートを使用します。

<import path="lib/isolink-connect/client.kscript" as="connect" />
import "lib/isolink-connect/client.kscript" as connect

組み込みのカタログや信頼済みの発行者に含まれない発行者の場合は、そのリポジトリを個別にプロジェクトへ登録してください。登録後は、名前だけを指定してインストールするか、その発行者を直接選択できます。たとえば、チームが slugs ライブラリを acme/kscript-libraries で公開している場合は、プロジェクトから両方のコマンドを実行してください。

コマンド機能
kscript repo add acme/kscript-librariesRepositories that publish libraries.
kscript lib add acme/kscript-libraries@slugsInstall its slugs library.

The first command also accepts the repository's HTTPS URL. It records the registration in .kscript/repos/ beside the nearest kscript.json, or in the current folder for standalone scripts. Commit the registration to share it with teammates. The second command downloads libraries/slugs/ from the registered repository's default branch. A script in the site root can then use import "lib/slugs/index.kscript" as slugs with the default layout.

For a direct install, kscript lib add owner/repo or kscript lib add https://github.com/owner/repo starts at the repository root. If its kscript.json points to a library folder, the installer copies just that folder. Otherwise it installs the entire repository. A GitHub tree URL selects a branch, tag, commit, or folder within it, such as kscript lib add https://github.com/owner/repo/tree/v1.0.0/libraries/slugs. Direct installs work without registering a repository.

Named libraries install into lib/<library-name>/. Direct installs use lib/<repository-name>/ when you select a repository or lib/<folder-name>/ when you select a folder. Set paths.lib in the nearest kscript.json to choose another folder relative to serve.root. Outside a project, the command installs into lib/ in the current folder. Imports are relative to the script containing them, so a script one level down would use ../lib/isolink-connect/client.kscript.

The installer reads the selected folder's kscript.json, validates its library metadata, and prints an import for library.entry. Without library.entry, the entry is the library's top-level main.kscript, main.ks.xml, index.kscript or index.ks.xml, in that order (the same main script rule as kscript run). For older libraries without a library section, it uses that rule too, then the shallowest script. The generated .kscript-lib.json receipt inside the installed folder records the source, exact commit and entry path, plus the name, version and runtime packages when declared.

In VS Code, completions inside the project's packages array show which runtime packages the installed, pinned libraries require. Add a package there when you also want it available throughout your project. Completions under types use the running KScript version's built-in types and accepted options, such as types.password.min and types.uuid.v.

Restoring a project's libraries

kscript lib add records the source URL and full commit under libraries in the consuming project's kscript.json. If there is no project yet, a successful add creates this file in the current directory. Commit it so teammates and CI can restore the same revisions after cloning.

A named Isolink install records the selected publisher folder and commit. For example, a project pinned to one revision looks like this.

JSON
{
  "libraries": {
    "isolink-connect": {
      "source": "https://github.com/waggr-gb/isolink-connect/tree/HEAD/libraries/isolink-connect",
      "commit": "d12745f9989a9523fcb33922f75625f0c1a96766"
    }
  }
}

On a fresh checkout, run the following command from the project or one of its subdirectories.

シェル
kscript lib restore

Restore reads the pins from the nearest project and fetches each library directly from its recorded publisher at the exact commit. For the example above, it fetches libraries/isolink-connect/ from waggr-gb/isolink-connect. A direct repository install records the repository URL and commit. Restore follows the library folder reference from that commit. If a team catalog publishes a Git submodule, the recorded catalog commit also fixes the upstream commit. An existing copy at the recorded revision keeps its local edits. To share those edits, commit the modified library files or publish the changes upstream. The installer uses the empty .kscript/libraries.lock file to coordinate simultaneous adds. You can ignore it in Git.

Publishing a named library

On the default branch of your public repository, put each library in an ordinary folder under libraries/ or pin a separate public GitHub repository there as a Git submodule. Both layouts publish source directly. For an ordinary folder, include the entry script, any scripts it imports by path, and any data it reads. The acme/kscript-libraries@slugs example could contain these files.

テキスト
libraries/
  slugs/
    kscript.json
    index.kscript
    README.md

Put the manifest beside the public source. For example, libraries/slugs/kscript.json can declare a Core-only library.

JSON
{
  "name": "slugs",
  "library": { "version": "1.0.0", "entry": "index.kscript" },
  "packages": []
}

library.version is a semantic version. library.entry is a .kscript or .ks.xml path relative to this file and can point to a nested module. Leave it out when the entry is main.kscript or index.kscript beside the manifest: "library": { "version": "1.0.0" } is enough. List required native runtime packages such as KScript.Net under packages, and import them in the source modules that use them. Declare public functions in the entry module. Users can then install your published library by its qualified name.

A repository that publishes a single library can place the same manifest and entry script at its root. Set name to the library name users should install. For example, a root manifest named slugs in the registered repository acme/slug-tools can be installed as kscript lib add acme/slug-tools@slugs.

A repository with tests or examples alongside the SDK can point its root kscript.json to the package folder. For example, Connect can keep its test settings in the following root manifest.

JSON
{
  "library": "libraries/isolink-connect",
  "test": { "include": ["tests/**/*.test.kscript"] }
}

The path is relative to this manifest. The target folder must contain its own kscript.json declaring a library name, library.version and library.entry. Declare any runtime packages there, too. Both kscript lib add waggr-gb/isolink-connect and kscript lib add waggr-gb/isolink-connect@isolink-connect then install that folder and print the same import. The repository's tests and examples stay in the checkout.

To publish slugs from a separate repository, run this in your library catalog and commit both .gitmodules and libraries/slugs.

シェル
git submodule add https://github.com/acme/slug-tools.git libraries/slugs

kscript lib add acme/kscript-libraries@slugs downloads the upstream commit pinned by the catalog. If the upstream contains libraries/slugs/, only that folder is installed, so other SDKs can share the repository. Otherwise the installer uses the upstream root. Keep all scripts and assets in the selected folder. The installer follows one submodule level, and records both repositories' commits in .kscript-lib.json. To publish an update, commit the new submodule pin in the catalog.

Edit this page on GitHub