vb.net 开发vsix扩展,实现less自动编译成为wxss

vb.net 开发vsix扩展,实现less自动编译成为wxss

扩展加载代码,因为我们这个主要目的是将less文件进行自动编译,所以我们这里在启时时不需要任何操作,就是空码就可以了,我写代码习惯把代码简洁化,所以我的代码都是比较少。

1、我的项目名为:LessBOM,以下是LessBOMPackage.vb的代码

Imports System
Imports System.Runtime.InteropServices
Imports System.Threading
Imports Microsoft.VisualStudio.Shell
Imports Task = System.Threading.Tasks.Task

<Guid("7F9FDCA5-FF41-4FED-8545-F2789636ED20"), PackageRegistration(UseManagedResourcesOnly:=True, AllowsBackgroundLoading:=True)>
Public NotInheritable Class LessBOMPackage
  Inherits AsyncPackage
  Protected Overrides Async Function InitializeAsync(cancellationToken As CancellationToken, progress As IProgress(Of ServiceProgressData)) As Task

  End Function
End Class

2、我建一个VsixCode.vb代码模块。

Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualStudio.Editor
Imports Microsoft.VisualStudio.Shell
Imports Microsoft.VisualStudio.Shell.Interop
Imports Microsoft.VisualStudio.Text
Imports Microsoft.VisualStudio.Text.Editor
Imports Microsoft.VisualStudio.TextManager.Interop
Imports Microsoft.VisualStudio.Utilities
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.Composition
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Text


<Export(GetType(IVsTextViewCreationListener)), ContentType("LESS"), TextViewRole(PredefinedTextViewRoles.PrimaryDocument)>
Public Class CommandReg
  Implements IVsTextViewCreationListener
  <Import>
  Private Property EditorAdaptersFactory As IVsEditorAdaptersFactoryService
  <Import>
  Private Property DocumentService As ITextDocumentFactoryService
  Dim Doc As ITextDocument


  Dim projet As Project, AppPath As String = ""
  Sub VsTextViewCreated(textViewAdapter As IVsTextView) Implements IVsTextViewCreationListener.VsTextViewCreated
    Dim textView As IWpfTextView = EditorAdaptersFactory.GetWpfTextView(textViewAdapter)
    If Not DocumentService.TryGetTextDocument(textView.TextDataModel.DocumentBuffer, Doc) Then
      Return
    End If
    AppPath = ServiceProvider.GlobalProvider.GetService(GetType(DTE)).Solution.FindProjectItem(Doc.FilePath).ContainingProject.FullName
    If AppPath = "" Then Return
    AddHandler Doc.FileActionOccurred, AddressOf DocSave
    AddHandler textView.Closed, AddressOf TextClosed '关闭事件
  End Sub
  Sub DocSave(sender As Object, e As TextDocumentFileActionEventArgs)
    If e.FileActionType <> FileActionTypes.ContentSavedToDisk Then Return
    If Not IO.File.Exists(AppPath & "app.less") Then Return

    Dim start = New ProcessStartInfo("cmd")
    start.WorkingDirectory = "C:\Program Files\nodejs\node_global\node_modules\less\bin\"
    start.UseShellExecute = False
    start.RedirectStandardOutput = True
    start.RedirectStandardError = True
    start.StandardOutputEncoding = Encoding.UTF8
    start.StandardErrorEncoding = Encoding.UTF8
    start.CreateNoWindow = True
    start.Arguments = "/c lessc " & AppPath & "app.less " & AppPath & "app.wxss"

    ' IO.File.AppendAllText("D:\kuaiguji\SoftLog.log", e.FilePath & AppPath & Environment.NewLine)

    Try
      Dim proc = Diagnostics.Process.Start(start)
      proc.WaitForExit()
    Catch ex As Exception
      ErrShow(ex)
    End Try
  End Sub

  Sub TextClosed(sender As Object, e As EventArgs)
    Dim textView As IWpfTextView = CType(sender, IWpfTextView)
    If textView IsNot Nothing Then RemoveHandler textView.Closed, AddressOf TextClosed '关闭事件
    If Doc IsNot Nothing Then RemoveHandler Doc.FileActionOccurred, AddressOf DocSave
  End Sub

  Dim ErrPane As IVsOutputWindowPane
  Sub ErrShow(msg As Object)
    If ErrPane Is Nothing Then
      Dim win As IVsOutputWindow = CType(ServiceProvider.GlobalProvider.GetService(GetType(SVsOutputWindow)), IVsOutputWindow), guid As System.Guid = System.Guid.NewGuid()
      win.CreatePane(guid, "Less", 1, 1)
      win.GetPane(guid, ErrPane)
    End If
    ErrPane.OutputString(DateTime.Now.ToString("HH:mm:ss") & " " & msg & Environment.NewLine)
  End Sub












End Class









代码原理则是在,打开文件时,获取工程的目录,我是将微信小程序的css全部编译到app.wxss,所以我这里代码写得有点死,但大家可 以根据代码去改进自已想要的。

3、就是在source.extension.vsixmanifest,对Assets进行添加Microsoft.VisualStudio.MefComponent选项,这样编译工程,就可以打包出来LessBOM.vsix了,安装上就可以使用了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值