33* Licensed under the MIT License. See License.txt in the project root for license information.
44*--------------------------------------------------------------------------------------------*/
55
6- import type { Template } from "@azure/arm-appcontainers" ;
6+ import { KnownActiveRevisionsMode , type Template } from "@azure/arm-appcontainers" ;
77import { nonNullValueAndProp } from "@microsoft/vscode-azext-utils" ;
88import { Disposable , Event , EventEmitter , FileChangeEvent , FileChangeType , FileStat , FileSystemProvider , FileType , TextDocument , Uri , window , workspace } from "vscode" ;
99import { URI } from "vscode-uri" ;
1010import { ext } from "../../extensionVariables" ;
1111import { ContainerAppItem } from "../../tree/ContainerAppItem" ;
1212import type { ContainerAppsItem } from "../../tree/ContainerAppsBranchDataProvider" ;
13- import type { RevisionDraftItem } from "../../tree/revisionManagement/RevisionDraftItem" ;
14- import type { RevisionItem } from "../../tree/revisionManagement/RevisionItem" ;
13+ import type { RevisionsItemModel } from "../../tree/revisionManagement/RevisionItem" ;
1514import { localize } from "../../utils/localize" ;
1615
1716const notSupported : string = localize ( 'notSupported' , 'This operation is not currently supported.' ) ;
@@ -50,23 +49,15 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
5049 return this . emitter . event ;
5150 }
5251
53- // Create
54- createRevisionDraft ( item : ContainerAppItem | RevisionItem | RevisionDraftItem ) : void {
52+ createRevisionDraft ( item : ContainerAppItem | RevisionsItemModel ) : void {
5553 const uri : Uri = this . buildUriFromItem ( item ) ;
5654 if ( this . draftStore . has ( uri . path ) ) {
5755 return ;
5856 }
5957
58+ // Branching path reasoning: https://github.com/microsoft/vscode-azurecontainerapps/blob/main/src/commands/revisionDraft/README.md
6059 let file : RevisionDraftFile | undefined ;
61- if ( item instanceof ContainerAppItem ) {
62- /**
63- * Sometimes there are micro-differences present between the latest revision template and the container app template.
64- * They end up being essentially equivalent, but not so equivalent as to always pass a deep copy test (which is how we detect unsaved changes).
65- *
66- * Since only container app template data is shown in single revision mode, and since revision data is not directly present on
67- * the container app tree item without further changes, it is easier to just use the container app template
68- * as the primary source of truth when in single revision mode.
69- */
60+ if ( ContainerAppItem . isContainerAppItem ( item ) || item . containerApp . revisionsMode === KnownActiveRevisionsMode . Single ) {
7061 const revisionContent : Uint8Array = Buffer . from ( JSON . stringify ( nonNullValueAndProp ( item . containerApp , 'template' ) , undefined , 4 ) ) ;
7162 file = new RevisionDraftFile ( revisionContent , item . containerApp . id , nonNullValueAndProp ( item . containerApp , 'latestRevisionName' ) ) ;
7263 } else {
@@ -78,8 +69,7 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
7869 this . fireSoon ( { type : FileChangeType . Created , uri } ) ;
7970 }
8071
81- // Read
82- parseRevisionDraft < T extends ContainerAppsItem > ( item : T ) : Template | undefined {
72+ parseRevisionDraft ( item : ContainerAppsItem ) : Template | undefined {
8373 const uri : URI = this . buildUriFromItem ( item ) ;
8474 if ( ! this . draftStore . has ( uri . path ) ) {
8575 return undefined ;
@@ -93,12 +83,12 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
9383 return contents ? Buffer . from ( contents ) : Buffer . from ( '' ) ;
9484 }
9585
96- doesContainerAppsItemHaveRevisionDraft < T extends ContainerAppsItem > ( item : T ) : boolean {
86+ doesContainerAppsItemHaveRevisionDraft ( item : ContainerAppsItem ) : boolean {
9787 const uri : Uri = this . buildUriFromItem ( item ) ;
9888 return this . draftStore . has ( uri . path ) ;
9989 }
10090
101- getRevisionDraftFile < T extends ContainerAppsItem > ( item : T ) : RevisionDraftFile | undefined {
91+ getRevisionDraftFile ( item : ContainerAppsItem ) : RevisionDraftFile | undefined {
10292 const uri : Uri = this . buildUriFromItem ( item ) ;
10393 return this . draftStore . get ( uri . path ) ;
10494 }
@@ -118,8 +108,7 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
118108 }
119109 }
120110
121- // Update
122- async editRevisionDraft ( item : ContainerAppItem | RevisionItem | RevisionDraftItem ) : Promise < void > {
111+ async editRevisionDraft ( item : ContainerAppItem | RevisionsItemModel ) : Promise < void > {
123112 const uri : Uri = this . buildUriFromItem ( item ) ;
124113 if ( ! this . draftStore . has ( uri . path ) ) {
125114 this . createRevisionDraft ( item ) ;
@@ -146,8 +135,17 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
146135 ext . state . notifyChildrenChanged ( file . containerAppId ) ;
147136 }
148137
149- // Delete
150- discardRevisionDraft < T extends ContainerAppsItem > ( item : T ) : void {
138+ updateRevisionDraftWithTemplate ( item : RevisionsItemModel , template : Template ) : void {
139+ const uri : Uri = this . buildUriFromItem ( item ) ;
140+ if ( ! this . draftStore . has ( uri . path ) ) {
141+ this . createRevisionDraft ( item ) ;
142+ }
143+
144+ const newContent : Uint8Array = Buffer . from ( JSON . stringify ( template , undefined , 4 ) ) ;
145+ this . writeFile ( uri , newContent ) ;
146+ }
147+
148+ discardRevisionDraft ( item : ContainerAppsItem ) : void {
151149 const uri : Uri = this . buildUriFromItem ( item ) ;
152150 if ( ! this . draftStore . has ( uri . path ) ) {
153151 return ;
@@ -161,8 +159,7 @@ export class RevisionDraftFileSystem implements FileSystemProvider {
161159 this . fireSoon ( { type : FileChangeType . Deleted , uri } ) ;
162160 }
163161
164- // Helper
165- private buildUriFromItem < T extends ContainerAppsItem > ( item : T ) : Uri {
162+ private buildUriFromItem ( item : ContainerAppsItem ) : Uri {
166163 return URI . parse ( `${ RevisionDraftFileSystem . scheme } :/${ item . containerApp . name } .json` ) ;
167164 }
168165
0 commit comments