|
| 1 | +--- |
| 2 | +description: Migrate code from the old ~/samples/snippets/ location to the relative ./snippets location. |
| 3 | +--- |
| 4 | + |
| 5 | +# Migrate code snippets |
| 6 | + |
| 7 | +**IMPORTANT**: Unless otherwise asked to, **only** edit the article file in context. At the end of your operations you may ask for permission to edit other articles referencing the same snippets. |
| 8 | + |
| 9 | +## Repository structure for code snippets |
| 10 | + |
| 11 | +**IMPORTANT**: This repository has TWO different locations for code snippets: |
| 12 | + |
| 13 | +### Old location (legacy - to be migrated FROM) |
| 14 | +- Path: `~/samples/snippets/` |
| 15 | +- Example: `~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/CS/form1.cs` |
| 16 | +- Status: Legacy, should be migrated to new location |
| 17 | + |
| 18 | +### New location (current standard - migrate TO) |
| 19 | +- Path pattern: `./snippets/{doc-file}/{code-language}/` |
| 20 | +- Example: `./snippets/how-to-add-data-to-the-clipboard/csharp/form1.cs` |
| 21 | + |
| 22 | +**Path components explained:** |
| 23 | +- `{doc-file}`: The markdown article filename WITHOUT the `.md` extension |
| 24 | + - Example: For article `how-to-add-data-to-the-clipboard.md` → use `how-to-add-data-to-the-clipboard` |
| 25 | +- `{code-language}`: |
| 26 | + - `csharp`: For C# code |
| 27 | + - `vb`: For Visual Basic code |
| 28 | + |
| 29 | +## Legacy code characteristics (migrate FROM these) |
| 30 | + |
| 31 | +**Location**: `~/samples/snippets/` folder |
| 32 | +**Problems with legacy code:** |
| 33 | +- Usually for .NET Framework (outdated) |
| 34 | +- Often incomplete or non-compilable |
| 35 | +- May lack project files |
| 36 | +- Uses outdated syntax and patterns |
| 37 | + |
| 38 | +**Legacy article references look like this:** |
| 39 | +```markdown |
| 40 | +[!code-{code-language}[description](~/samples/snippets/{path-to-file}#{snippet-identifier})] |
| 41 | +``` |
| 42 | +It's possible that the reference was already migrated, but the code wasn't, which looks like this: |
| 43 | +```markdown |
| 44 | +:::code language="{code-language}" source="~/samples/snippets/{path-to-file}" id="{snippet-identifier}"::: |
| 45 | +``` |
| 46 | +Based on the {path-to-file} you can determine if it's in the old system or not. |
| 47 | + |
| 48 | +## Current code requirements (migrate TO these) |
| 49 | + |
| 50 | +**Location**: `./snippets/{doc-file}/{code-language}/` |
| 51 | + |
| 52 | +**Requirements for current code standards:** |
| 53 | +- ✅ MUST be complete and compilable |
| 54 | +- ✅ MUST include a project file |
| 55 | +- ✅ MUST target appropriate .NET version (see targeting rules below) |
| 56 | +- ✅ MUST provide BOTH C# and Visual Basic versions |
| 57 | +- ✅ MUST use appropriate syntax for the target framework |
| 58 | +- ✅ MUST use meaningful, descriptive snippet identifiers in CamelCase format |
| 59 | + - **Examples** of good snippet identifiers: `BasicClipboardData`, `CustomDataFormat`, `ClipboardImageHandling` |
| 60 | + - **Avoid** simplistic identifiers like `1`, `2`, `code1`, or `snippet1` |
| 61 | + |
| 62 | +**Current article references look like this:** |
| 63 | +```markdown |
| 64 | +:::code language="{code-language}" source="{file-path}" id="{snippet-identifier}"::: |
| 65 | +``` |
| 66 | + |
| 67 | +**Framework targeting rules:** |
| 68 | +- **Migration vs. Modernization**: |
| 69 | + - **Migration**: Move code to new location with minimal changes |
| 70 | + - **Modernization**: Update code to use latest .NET features and best practices |
| 71 | +- **When to modernize**: Unless told not to modernize, always modernize from .NET Framework to the latest .NET |
| 72 | + |
| 73 | +## Migration steps (follow in order) |
| 74 | + |
| 75 | +**WHEN TO MIGRATE**: Migrate code when you encounter articles with references to `~/samples/snippets/` paths. |
| 76 | + |
| 77 | +**STEP-BY-STEP PROCESS:** |
| 78 | + |
| 79 | +### 1. Analyze existing code and article context |
| 80 | +- **Find**: Locate the legacy snippet file in `~/samples/snippets/` |
| 81 | +- **Identify**: Determine the programming language (C# or Visual Basic) |
| 82 | +- **Extract**: Note the snippet identifier used in the article reference |
| 83 | + |
| 84 | +### 2. Create new folder structure |
| 85 | +- **Pattern**: `./snippets/{doc-file}/{code-language}/` |
| 86 | +- **Example**: For article `clipboard-operations.md` → create `./snippets/clipboard-operations/csharp/` |
| 87 | + |
| 88 | +### 3. Migrate and update code |
| 89 | +- **Copy**: Copy only the snippet code (and any supporting code to compile the snippet) to the new location |
| 90 | +- **Complete**: Ensure code is fully functional and compilable |
| 91 | +- **Project file**: Create or update project file with appropriate target framework |
| 92 | + |
| 93 | +### 4. Create both language versions |
| 94 | +- **Requirement**: MUST provide both C# and Visual Basic versions |
| 95 | +- **C# path**: `./snippets/{doc-file}/csharp/` |
| 96 | +- **VB path**: `./snippets/{doc-file}/vb/` |
| 97 | + |
| 98 | +### 5. Update article references |
| 99 | +- **Replace**: Change from legacy `[!code-...]` format to modern `:::code...:::` format |
| 100 | +- **Before**: `[!code-csharp[description](~/samples/snippets/path/file.cs#snippet1)]` |
| 101 | +- **After**: `:::code language="csharp" source="./snippets/doc-name/csharp/file.cs" id="BasicClipboardData":::` |
| 102 | +- **Note**: Use meaningful CamelCase identifiers instead of simple numbers |
| 103 | + |
| 104 | +### 6. Validate |
| 105 | +- **Build**: Ensure all code compiles successfully |
| 106 | +- **Test**: Verify snippet references work in the article |
| 107 | + |
| 108 | +### 7. Delete |
| 109 | +- **Identify**: |
| 110 | + - Check if the old snippet file is used by any other articles |
| 111 | + - Some articles may use a relative path to the `samples` folder, so simply search for links to `samples/snippets/...` |
| 112 | + - Be confident that all legacy snippets use a `samples/snippets` folder structure |
| 113 | +- **Delete**: If old snippet is no longer used by any article, delete it. |
| 114 | + |
| 115 | +## Common mistakes to avoid |
| 116 | + |
| 117 | +- ❌ **Don't** assume all code needs to be modernized - check article context first |
| 118 | +- ❌ **Don't** modernize .NET Framework-specific articles unless specifically requested |
| 119 | +- ❌ **Don't** forget to create both C# and VB versions |
| 120 | +- ❌ **Don't** forget to update ALL article references to the migrated code |
| 121 | +- ❌ **Don't** leave incomplete or non-compilable code |
0 commit comments