演習 - GitHub Copilot ツールを使用してコードを分析、文書化する
GitHub Copilot は、説明やドキュメントを生成することで、コードベースを理解して文書化するのに役立ちます。 この演習では、GitHub Copilot を使用してライブラリ アプリケーションを分析し、文書化します。 また、ライブラリ アプリケーション用の GitHub リポジトリを作成し、GitHub Copilot を使用してリポジトリの README ファイルを生成します。
この演習には、次のタスクが含まれています。
- GitHub Copilot を使用して、ライブラリ アプリケーションのコードベースを説明する。
- Visual Studio Code を使用して、ライブラリ アプリケーション用の GitHub リポジトリを作成する。
- GitHub Copilot 拡張機能を使用してソリューションを分析し、リポジトリの README.md ファイルを作成する。
ライブラリ アプリケーションのコードベースを調べる
コード リポジトリと README ファイルを作成する前に、少し時間を取って、コードベースを調べる必要があります。 プロジェクトを文書化する前に、アプリのアーキテクチャと主な機能を理解しておくことが重要です。 また、アプリケーションを実行して、期待どおりに動作するようにする必要もあります。
GitHub Copilot を使用してライブラリ アプリケーションのコードベースを説明する
GitHub Copilot は、ソリューションと機能レベルで説明を生成することで、未知のコードベースを理解するのに役立ちます。
以下の手順に従って、演習のこのセクションを完了します。
Visual Studio Code で
AccelerateDevGitHubCopilot
ソリューションを開いていることを確認します。チャット ビューを開き、GitHub Copilot の
@workspace
参加者を使用してプロジェクトの説明を生成するプロンプトを入力します。たとえば、チャット ビューに次のプロンプトを入力します。
@workspace Describe this project
数分かけて応答を確認します。
次の説明のような応答が表示されます。
This project is a library management system implemented in C#. It consists of three main components: 1. **Library.ApplicationCore**: This is the core application library containing the main business logic, entities, enums, and interfaces. It includes: - Entities such as Book, Patron, Loan, etc. - Enums like MembershipRenewalStatus, LoanReturnStatus, and LoanExtensionStatus. - Services like LoanService and PatronService. - Interfaces like ILoanRepository, IPatronRepository, ILoanService, and IPatronService. 2. **Library.Console**: This is a console application that interacts with the user. It includes: - The main entry point in Program.cs. - Configuration settings in appSettings.json. - Console interaction logic in ConsoleApp.cs. 3. **Library.Infrastructure**: This library handles data persistence. It includes: - JSON-based repositories like JsonPatronRepository and JsonLoanRepository. - Data loading and saving logic in JsonData. Additionally, there are unit tests located in the UnitTests directory, which test the functionality of the core services using the NSubstitute mocking framework and xUnit. The project uses .NET 8.0 and is configured to build and test using GitHub Actions as defined in the build-test.yml file. The solution file is AccelerateDevGitHubCopilot.sln, which includes all the projects and their configurations.
ソリューション エクスプローラー ビューを使用して、プロジェクト フォルダーを展開します。
ConsoleApp.cs
ファイルを見つけて開きます。ConsoleApp.cs ファイルは、
src/Library.Console
フォルダーにあります。ConsoleApp
クラスの説明を生成するプロンプトをチャット ビューに入力します。たとえば、チャット ビューに次のプロンプトを入力します。
@workspace #editor Describe the ConsoleApp class
数分かけて応答を確認します。
次の説明のような応答が表示されます。
The `ConsoleApp` class in `ConsoleApp.cs` is responsible for managing the console-based user interface of the library management system. It handles user interactions, processes user inputs, and coordinates actions with the underlying services and repositories. ### Fields - `_currentState`: Tracks the current state of the console application. - `matchingPatrons`: A list of patrons that match a search query. - `selectedPatronDetails`: Holds details of the currently selected patron. - `selectedLoanDetails`: Holds details of the currently selected loan. - `_patronRepository`: An instance of `IPatronRepository` for accessing patron data. - `_loanRepository`: An instance of `ILoanRepository` for accessing loan data. - `_loanService`: An instance of `ILoanService` for performing loan-related operations. - `_patronService`: An instance of `IPatronService` for performing patron-related operations. ### Constructor - `ConsoleApp(ILoanService loanService, IPatronService patronService, IPatronRepository patronRepository, ILoanRepository loanRepository)`: Initializes the `ConsoleApp` with the provided services and repositories. ### Methods - `Run()`: The main entry point for running the console application. - `PatronSearch()`: Handles the logic for searching patrons. - `ReadPatronName()`: Reads the patron's name from the console input. - `PrintPatronsList(List<Patron> matchingPatrons)`: Prints a list of matching patrons to the console. - `PatronSearchResults()`: Handles the logic for displaying and selecting from the search results. - `ReadInputOptions(CommonActions options, out int optionNumber)`: Reads input options from the console. - `WriteInputOptions(CommonActions options)`: Writes the available input options to the console. - `PatronDetails()`: Displays details of the selected patron. - `LoanDetails()`: Displays details of the selected loan and handles actions like extending or returning the loan. The class uses a state machine approach to manage different states of the console application, such as searching for patrons, displaying search results, showing patron details, and handling loan details.
Program.cs
ファイルを開きます。Program.cs
ファイルの説明を生成するプロンプトをチャット ビューに入力します。たとえば、チャット ビューに次のプロンプトを入力します。
@workspace #editor Describe the Program.cs file
数分かけて応答を確認します。
次の説明のような応答が表示されます。
The `Program.cs` file is the entry point for the console application. It sets up the dependency injection container, configures services, and starts the `ConsoleApp`. 1. **Dependency Injection Setup**: - Creates a `ServiceCollection` to register services. - Builds a configuration object from the `appSettings.json` file. - Registers various services and repositories with different lifetimes (singleton, scoped). 2. **Service Registration**: - Registers the repositories: JsonPatronRepository and JsonLoanRepository. - Registers the services: LoanService and PatronService. - Registers the JsonData class as a singleton. - Registers the ConsoleApp class as a singleton. 3. **Service Provider**: - Builds the service provider from the registered services. - Retrieves the `ConsoleApp` service and runs it asynchronously. This setup ensures that all dependencies are properly injected and configured before the application starts running.
Library.Infrastructure プロジェクト フォルダーを展開します。
JsonData.cs
、JsonLoanRepository.cs
、およびJsonPatronRepository.cs
ファイルをチャット コンテキストに追加します。[ コンテキストのアタッチ ] ボタンを使用して、チャット コンテキストにファイルを追加します。 ドラッグ アンド ドロップ操作を使用して、チャット コンテキストにファイルを追加することもできます。
データ アクセス クラスの説明を生成するプロンプトをチャット ビューに入力します。
たとえば、チャット ビューに次のプロンプトを入力します。
@workspace /explain Explain how the data access classes work
数分かけて応答を確認します。
次の説明のような応答が表示されます。
The `JsonData` class in `JsonData.cs` is responsible for managing the loading and saving of data from JSON files. It handles various entities such as `Author`, `Book`, `BookItem`, Patron`, and `Loan`. The class uses the `IConfiguration` interface to read file paths from the configuration settings, ensuring that the correct JSON files are loaded. The constructor of the `JsonData` class initializes the file paths for each entity by reading from the configuration section "JsonPaths". If the paths are not specified in the configuration, it defaults to specific paths within a "Json" directory. The `EnsureDataLoaded` method checks if the data is already loaded and, if not, calls the `LoadData` method to load the data from the JSON files. The `LoadData` method reads the JSON files and deserializes them into the respective lists of entities. The `SaveLoans` and `SavePatrons` methods handle saving the loan and patron data back to the JSON files. These methods ensure that only a subset of properties is saved, maintaining data integrity and consistency. The class also provides methods to populate entities with related data. For example, `GetPopulatedPatrons` and `GetPopulatedPatron` methods populate patron entities with their associated loans. Similarly, `GetPopulatedLoan` and `GetPopulatedBookItem` methods populate loan and book item entities with their related data. The `LoadJson` and `SaveJson` methods are utility methods for reading from and writing to JSON files. They use the `JsonSerializer` class to handle the serialization and deserialization processes. Overall, the `JsonData` class serves as a central point for managing the persistence of data in JSON format, ensuring that the application can load, save, and manipulate data efficiently.
数分間をかけて、ライブラリ レコードのシミュレートに使用される JSON データ ファイルを調べます。
JSON データ ファイルは、
src/Library.Console/Json
フォルダーにあります。データ ファイルは、ID プロパティを使用してエンティティをリンクします。 たとえば、
Loan
オブジェクトには、同じ ID を持つPatronId
オブジェクトにリンクするPatron
プロパティがあります。 JSON ファイルには、著者、書籍、書籍アイテム、パトロン、ローンのデータが含まれています。注
このガイド付きプロジェクトの目的上、著者名、書籍タイトル、パトロン名は匿名になっています。
アプリケーションの構築と実行
アプリケーションを実行すると、ユーザー インターフェイス、アプリケーションの主な機能、アプリ コンポーネントの相互作用を理解するのに役立ちます。
以下の手順に従って、演習のこのセクションを完了します。
ソリューション エクスプローラー ビューが開かれていることを確認します。
ソリューション エクスプローラー ビューは、エクスプローラー ビューと同じではありません。 ソリューション エクスプローラー ビューは、プロジェクト ファイルとソリューション ファイルを "ディレクトリ" ノードとして使用して、ソリューションの構造を表示します。
アプリケーションを実行するには、[ Library.Console] を右クリックし、[ デバッグ] を選択し、[ 新しいインスタンスの開始] を選択します。
[デバッグ] オプションと [新しいインスタンスの開始] オプションが表示されない場合は、エクスプローラー ビューではなくソリューション エクスプローラー ビューを使用していることを確認します。
次の手順では、簡単なユース ケースについて説明します。
パトロン名の入力を求められたら、「 One」 と入力し、Enter キーを押します。
検索クエリに一致するパトロンの一覧が表示されます。
注
アプリケーションは、大文字と小文字を区別する検索プロセスを使用します。
「入力オプション」プロンプトで「 2 」と入力し、Enter キーを押します。
「2」と入力すると、リスト内の 2 番目のパトロンが選択されます。
パトロンの名前とメンバーシップの状態が表示され、その後に書籍ローンの詳細が表示されます。
[入力オプション] プロンプトで、「 1 」と入力し、Enter キーを押します。
「1」と入力すると、リスト内の最初の書籍が選択されます。
期限や返却状況など、書籍の詳細が一覧表示されます。
[入力オプション] プロンプトで、「 r 」と入力し、Enter キーを押します。
r を入力すると、書籍が返されます。
"Book was successfully returned." というメッセージが表示されていることを確認します。
"Book was successfully returned." というメッセージの後に、書籍の詳細が続けて表示されるはずです。 返された書籍は
Returned: True
でマークされます。新しい検索を開始するには、「 s 」と入力し、Enter キーを押します。
パトロン名の入力を求められたら、「 One」 と入力し、Enter キーを押します。
「入力オプション」プロンプトで「 2 」と入力し、Enter キーを押します。
最初の書籍ローンが
Returned: True
とマークされていることを確認します。[入力オプション] プロンプトで、「 q 」と入力し、Enter キーを押します。
デバッグ セッションを停止します。
コード用の GitHub リポジトリを作成する
コード用の GitHub リポジトリを作成すると、他のユーザーと作業を共有したり、プロジェクトで共同作業を行うことができます。
注
自分の GitHub アカウントを使用して、ライブラリ アプリケーション用のプライベート GitHub リポジトリを作成してください。
以下の手順に従って、演習のこのセクションを完了します。
ブラウザー ウィンドウを開き、GitHub アカウントに移動します。
GitHub ログイン ページは https://github.com/login です。
GitHub アカウントにサインインします。
GitHub アカウント メニューを開き、[ リポジトリ] を選択します。
[Visual Studio Code] ウィンドウに切り替えます。
Visual Studio Code で、ソース管理ビューを開きます。
[ GitHub に発行] を選択します。
リポジトリの既定の名前 (AccelerateDevGitHubCopilotSelect) をそのまま使用し、[ GitHub プライベート リポジトリに発行] を選択します。
注
Visual Studio Code で GitHub にサインインしていない場合は、サインインするように求められます。 サインインしたら、要求されたアクセス許可で Visual Studio Code を認可します。
ソース管理ビューで、"初期コミット" などのコミット メッセージを入力し、[ ブランチの発行] を選択します。
公開プロセス中に、Visual Studio Code にステータス メッセージが表示されている点に注目します。
公開プロセスが完了すると、指定した GitHub リポジトリにコードが正常に公開されたことを示すメッセージが表示されます。
GitHub アカウントのブラウザー ウィンドウに切り替えます。
GitHub アカウントで新しい AccelerateDevGitHubCopilot リポジトリを開きます。
AccelerateDevGitHubCopilot リポジトリが表示されない場合は、ページを更新します。 それでもリポジトリが表示されない場合は、次の手順を試してください。
- Visual Studio Code に切り替えます。
- 通知を開きます (通知は、新しいリポジトリが公開されたときに生成されています)。
- [GitHub で開く] を選択してリポジトリを開きます。
AccelerateDevGitHubCopilot リポジトリの [コード] タブで、[ README の追加] を選択します。
README.md エディターで、「 近日公開 」と入力し、[変更の コミット] を選択します。
Commit changes
ダイアログで、[変更のコミット] を選択します。Visual Studio Code に切り替え、ソース管理ビューが開かれていることを確認します。
[ ビューとその他のアクション] メニューを開き、[ プル] を選択します。
[ビューとその他のアクション] メニューは、ソース管理ビューの右上隅にある省略記号を使用することで開けます。
(ソリューション エクスプローラーではなく) エクスプローラー ビューを開き、 AccelerateDevGitHubCopilot フォルダーを展開します。
README.md ファイルを開きます。
"Coming soon" というメッセージが表示されます。
この演習の次のセクションでは、GitHub Copilot Chat を使用してリポジトリの README ファイルを更新します。
README ファイルのプロジェクト ドキュメントを作成する
README ファイルは、GitHub リポジトリの重要な部分です。 README は、プロジェクト、プロジェクトの共同作成者、および利害関係者のニーズに基づいて情報を提供します。
このガイド付きプロジェクトの演習では、README ファイルに次のセクションが含まれている必要があります。
- プロジェクト タイトル: プロジェクトの簡潔で明確なタイトル。
- 説明: プロジェクトの内容と実行内容の詳細な説明。
- プロジェクト構造: キー フォルダーとファイルを含むプロジェクト構造の内訳。
- キー クラスとインターフェイス: プロジェクト内の主要なクラスとインターフェイスの一覧。
- 使用法: プロジェクトの使用方法に関する手順 (多くの場合、コード例を含む)。
- ライセンス: プロジェクトの対象となるライセンス。
演習のこのセクションでは、GitHub Copilot を使用してプロジェクト ドキュメントを作成し、README.md
ファイルに追加します。
以下の手順に従って、演習のこのセクションを完了します。
チャット ビューを開きます。
ドラッグ アンド ドロップ操作を使用して、チャット ビュー コンテキストにファイルを追加します。
README ファイルのプロジェクト ドキュメントを生成するには、次のプロンプトを入力します。
@workspace Generate the contents of a README.md file for the code repository. Use "Library App" as the project title. The README file should include the following sections: Description, Project Structure, Key Classes and Interfaces, Usage, License. Format all sections as raw markdown. Use a bullet list with indents to represent the project structure. Do not include ".gitignore" or the ".github", "bin", and "obj" folders.
注
複数のプロンプトを使用すると、README ファイルのセクションごとに 1 つずつ、より詳細な結果が生成されます。 この演習では、プロセスを簡略化するために 1 つのプロンプトを使用します。
応答を確認して、各セクションがマークダウンとして書式設定されていることを確認します。
注
正しく書式設定されていない場合は、セクションを個別に更新して、より詳細な情報を提供できます。 GitHub Copilot の応答を README ファイルにコピーし、マークダウン ファイルで直接修正することもできます。
推奨されるドキュメント セクションを README.md ファイルにコピーします。
# Library App ## Description Library App is a console-based application for managing library operations such as patron management, book loans, and membership renewals. It uses a JSON-based data storage system and provides various services to handle library functionalities. ## Project Structure - AccelerateDevGitHubCopilot.sln - README.md - src - `Library.ApplicationCore/` - `Entities/` - `Author.cs` - `Book.cs` - `BookItem.cs` - `Loan.cs` - `Patron.cs` - `Enums/` - `LoanExtensionStatus.cs` - `LoanReturnStatus.cs` - `MembershipRenewalStatus.cs` - `Interfaces/` - `IPatronRepository.cs` - `IPatronService.cs` - `ILoanRepository.cs` - `ILoanService.cs` - `Library.ApplicationCore.csproj` - `Services/` - `PatronService.cs` - `LoanService.cs` - `Library.Console/` - `appSettings.json` - `CommonActions.cs` - `ConsoleApp.cs` - `ConsoleState.cs` - `Library.Console.csproj` - `Program.cs` - `Library.Infrastructure/` - `Data/` - `JsonData.cs` - `JsonLoanRepository.cs` - `JsonPatronRepository.cs` - `Library.Infrastructure.csproj` - tests - `UnitTests/` - `ApplicationCore/` - `LoanService/` - `ReturnLoan.cs` - `ExtendLoan.cs` - `PatronService/` - `RenewMembership.cs` - `UnitTests.csproj` ## Key Classes and Interfaces - **Entities** - **Author**: Represents an author with properties like `Id` and `Name`. - **Book**: Represents a book with properties like `Id`, `Title`, `AuthorId`, `Genre`, `ImageName`, and `ISBN`. - **BookItem**: Represents a specific copy of a book with properties like `Id`, `BookId`, `AcquisitionDate`, and `Condition`. - **Loan**: Represents a loan with properties like `Id`, `BookItemId`, `PatronId`, `LoanDate`, `DueDate`, and `ReturnDate`. - **Patron**: Represents a library patron with properties like `Id`, `Name`, `MembershipStart`, `MembershipEnd`, and `Loans`. - **Enums** - **LoanExtensionStatus**: Enum for loan extension statuses. - **LoanReturnStatus**: Enum for loan return statuses. - **MembershipRenewalStatus**: Enum for membership renewal statuses. - **Interfaces** - **IPatronRepository**: Interface for patron repository with methods like `GetPatron`, `SearchPatrons`, and `UpdatePatron`. - **IPatronService**: Interface for patron service with method `RenewMembership`. - **ILoanRepository**: Interface for loan repository with methods like `GetLoan` and `UpdateLoan`. - **ILoanService**: Interface for loan service with methods like `ReturnLoan` and `ExtendLoan`. - **Services** - **PatronService**: Implements `IPatronService` to handle patron-related operations. - **LoanService**: Implements `ILoanService` to handle loan-related operations. - **Console** - **ConsoleApp**: Main console application class that handles user interactions and application flow. - **ConsoleState**: Enum for different states of the console application. - **CommonActions**: Enum for common actions in the console application. - **Infrastructure** - **JsonData**: Handles loading and saving data to JSON files. - **JsonLoanRepository**: Implements `ILoanRepository` to manage loan data. - **JsonPatronRepository**: Implements `IPatronRepository` to manage patron data. ## Usage 1. Clone the repository. 2. Open the solution file AccelerateDevGitHubCopilot.sln in Visual Studio. 3. Build the solution to restore dependencies. 4. Run the `Library.Console` project to start the console application. 5. Follow the on-screen instructions to search for patrons, view patron details, extend loans, return books, and renew memberships. ## License This project is licensed under the MIT License.
必要に応じて書式設定を手動で調整し、更新された Readme.md ファイルを保存します。
Visual Studio Code の [ソース管理] ビューを開きます。
GitHub Copilot によってコミット メッセージが生成されるようにするには、メッセージ ボックスの右側にあるアイコンを選択します。
次のようなコミット メッセージが表示されます。
chore: Update README.md with project description and usage instructions
ファイルの更新をステージングしてコミットします。
GitHub リポジトリに更新を同期 (またはプッシュ) します。
作業を確認
作業内容を確認するには、次の手順を実行します。
AccelerateDevGitHubCopilot プロジェクトの GitHub リポジトリを開きます。
[ コード ] タブを使用して、更新された README.md ファイルを確認します。
README ファイルで説明されているプロジェクト構造が、リポジトリのフォルダー構造と一致していることを確認します。
コミット履歴を確認し、GitHub Copilot によって生成されたコミット メッセージを見つけます。