/* * Copyright 2019 faddenSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Diagnostics; namespace SourceGenWPF.AsmGen { public class AssemblerVersion { /// /// Version string reported by the assembler. Retained mostly for debugging. /// public string VersionStr { get; private set; } /// /// Version string converted to a Version object. For very complex version strings, /// some information may be lost in the conversion. /// public CommonUtil.Version Version { get; private set; } //// Command pathname and modification date. Useful for caching values. //private string ExeName { get; set; } //private DateTime ExeModWhen { get; set; } public AssemblerVersion(string versionStr, CommonUtil.Version version) { VersionStr = versionStr; Version = version; } public static AssemblerVersion GetVersion(AssemblerInfo.Id id) { IAssembler asm = AssemblerInfo.GetAssembler(id); if (asm == null) { Debug.WriteLine("Assembler " + id + " not configured"); return null; } return asm.QueryVersion(); } public override string ToString() { return "['" + VersionStr + "'/" + Version + "]"; } } /// /// Maintains a cache of the versions of installed assemblers. /// public static class AssemblerVersionCache { private static Dictionary sVersions = new Dictionary(); private static bool sQueried = false; /// /// Queries the versions from all known assemblers, replacing any previously held data. /// public static void QueryVersions() { IEnumerator iter = AssemblerInfo.GetInfoEnumerator(); while (iter.MoveNext()) { AssemblerInfo.Id id = iter.Current.AssemblerId; if (id == AssemblerInfo.Id.Unknown) { continue; } AssemblerVersion vers = null; IAssembler asm = AssemblerInfo.GetAssembler(id); if (asm != null) { vers = asm.QueryVersion(); } Debug.WriteLine("Asm version query: " + id + "=" + vers); sVersions[id] = vers; } sQueried = true; } /// /// Returns the version information, or null if the query failed for this assembler. /// /// Assembler identifier. /// Version info. public static AssemblerVersion GetVersion(AssemblerInfo.Id id) { if (!sQueried) { QueryVersions(); } if (sVersions.TryGetValue(id, out AssemblerVersion vers)) { return vers; } else { return null; } } } }