Sunday, June 14, 2020

Building Firefox on Windows 10

Building Firefox

The build tools linked here https://firefox-source-docs.mozilla.org/setup/windows_build.html do not like spaces in their paths.

For example, ./mach build step will fail with an error: "ERROR: GetShortPathName returned a long path name for C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Hostx64\x64\cl.exe" or something like that. A number of solutions suggested here, https://bugzilla.mozilla.org/show_bug.cgi?id=1323381.

This is discussed here https://bugzilla.mozilla.org/show_bug.cgi?id=1323381 I personally do not like enable short names using fsutil because it will slow down directory enumeration. Here is what worked for me,
  • Create a link, a directory junction https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/cc753194(v=ws.11). The link will obviously have a shorter name with no spaces, for example Mklink /j C:\VS2019 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community The paths as shown in the example is what worked for me

  • Add a new environment path VC_PATH with value C:\VS2019\VC\Tools\MSVC\14.26.28801 make sure the value is actually a valid directory and update it to the latest if necessary.

  • Run (or rerun) ./mach configure. You may have to delete the config.cache file. (There is probably a command to clear a config)

  • Run ./mach build. My build was successful


Summary

  • Mklink /j C:\VS2019 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
  • set VC_PATH=C:\VS2019\VC\Tools\MSVC\14.26.28801 or add it throw the GUI to persist it
  • ./mach configure (you have to delete the existing config.cache
  • ./mach build

Links


Saturday, April 11, 2020

Configure NuGet Cache Directories

Powershell snippets to change where nuget caches packages. As they are, these lines will add a machine-wide environment variable.

[System.Environment]::SetEnvironmentVariable('NUGET_PACKAGES', 'D:\nuget\packages', [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('NUGET_HTTP_CACHE_PATH', 'D:\nuget\v3-cache', [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('NUGET_PLUGINS_CACHE_PATH', 'D:\nuget\plugins-cache', [System.EnvironmentVariableTarget]::Machine)
For more https://docs.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders

Monday, February 29, 2016

AngularJS: Change in Underlying Model Not Reflected in View


Struggling with an update to the view model that among other things affects the visibility of a DOM element. While the DOM element became visible, some of its properties where not updated. The DOM properties that were not updated include offset.top and offset.left. They were always zero even after the DOM element became visible.

This is perhaps because updating the DOM properties is not instantaneous but rather does take time. What comes to mind is the question of what if you force update to propagate?

So $scope.$apply will indirectly cause watchers of the model's properties to be notified of changes. $scope.$apply internally calls $digest and the documentation does not recommend calling $digest directly.

Therefore calling $scope.$apply seemingly fixes the issue. It, however, does not seem a recommended practice. See discussion here http://stackoverflow.com/questions/12729122/angularjs-prevent-error-digest-already-in-progress-when-calling-scope-apply

In my case what worked is waiting for the DOM to updated. The amount of time wait was chosen based on experimentation. The way to wait is using setTimeout or in the angular way $timeout.

Saturday, December 6, 2014

Is Perfect Square for BigInteger

For testing if an integer is a perfect square


   private bool IsPerfectSquare(BigInteger n)
   {
        
        var sqrt =  (BigInteger)Math.Round(Math.Exp(BigInteger.Log(n) / 2));
 return (sqrt * sqrt) == n;
    }
I took it from http://msdn.microsoft.com/en-us/library/dd268263(v=vs.110).aspx

Thursday, September 11, 2014

An Advantage of Using Current SynchronizationContext over Control.BeginInvoke

SynchronizationContext vs Control.BeginInvoke


Windows Forms and WPF framework do not allow modifications to UI elements and controls from a non-UI thread. So if a task thread wants to display a result on the UI or change a control to indicate state of some sort it has to somehow communicate with the UI thread and the UI thread should take care of displaying the UI updates, carry out the desired change to UI controls, etc.

There are two to accomplish this:

  • Capturing the SynchronizationContext as in the code snippet below which is assumed to part of a method in child class of the System.Windows.Form class:
      Task.Factory.StartNew(() =>
      {
        // update UI here
      }, this.cancelToken.Token
       , TaskCreationOptions.None
       , TaskScheduler.FromCurrentSynchronizationContext()
     );
    

  • Using Control.BeginInvok as in the code snippet below which also assumes to be with a child class of the the System.Windows.Form class:
    Task.Factory.StartNew(() =>
    {
        DoSomeWrok();
        Action updateUIDelegate = () =>{ UpdateUI();}; 
        /* ^^ a way of creating a delegate to be passed to BeginInvoke */
        this.BeginInvoke(updateUIDelegate); //will be execute on the UI thread.
        /* ^ this refers to the Form instance */
    }, this.cancelToken.Token
    );
    

    Notice in the above code we did not need to capture the current SynchronizationContext.

One Reason to Prefer Capturing Current SynchronizationContext:


Control.BeginInvok is not available in WPF. So if you are creating classes to be used in both Windows Forms and WPF then using SynchronizationContext is the portable way. Here is more from http://blogs.msdn.com/b/pfxteam/archive/2012/06/15/executioncontext-vs-synchronizationcontext.aspx:
We now have two different APIs for achieving the same basic operation, so how do I write my component to be agnostic of the UI framework?  By using SynchronizationContext.  SynchronizationContext provides a virtual Post method; this method simply takes a delegate and runs it wherever, whenever, and however the SynchronizationContext implementation deems fit.  Windows Forms provides the WindowsFormSynchronizationContext type which overrides Post to call Control.BeginInvoke.  WPF provides the DispatcherSynchronizationContext type which overrides Post to call Dispatcher.BeginInvoke.  And so on.  As such, I can now code my component to use SynchronizationContext instead of tying it to a specific framework.
I was curios how WindowsFormsSynchronizationContext mentioned in the article mentioned above implemented the Post method so I took a look at the code (reflected):


public override void Post(SendOrPostCallback d, object state)
{
 if (this.controlToSendTo != null)
 {
  this.controlToSendTo.BeginInvoke(d, new object[]
  {
   state
  });
 }
}

Tuesday, September 9, 2014

Rendering Partial View from C#

Rendering Partial View from Inside a Controller Action in C#:

Sometimes I find a need to a rendered HTML string from inside an action (a response to AJAX call for example).

The RenderPartialView function below assumes it's a member method of a class derived from the System.Web.Mvc.Controller class.

Function description: Renders the partial view and returns the generated Html. I think it's easier than returning the raw data and having jQuery generates the HTML. Params: partialViewFullPath - The path and extension to the partial view. for example, ~/Views/Controller/ViewName.cshtml    model - Almost always we expect a model Returns: HTML string generated by the the MVC framework itself.

   
public string RenderPartialView(string partialViewFullPath, object model){
    /* to avoid null ref exception when unit testing */
    if (HttpContext == null || HttpContext.Request == null)
    {
        return string.Empty;
    }
    if (string.IsNullOrEmpty(partialViewFullPath))
    {
        throw new ArgumentException("partialViewName");
    }
    ViewDataDictionary viewData = new ViewDataDictionary(model);
    var razorView = new RazorView(this.ControllerContext, partialViewFullPath
    , null, false, new string[] { "cshtml" });
    var sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw))
        {
            ViewContext viewContext = new ViewContext(this.ControllerContext
                            ,razorView,viewData, this.TempData, tw);
            razorView.Render(viewContext, tw);
        }
    }
    return sb.ToString();
}


The RenderPartialView function can be called as the following example code shows

        
[HttpPost]
public ActionResult SomeAction(int id)
{
    ///...
    var result = new AjaxReturnObjectModel();
    result.Success = true;
    result.Html = RenderPartialView("~/Views/Controller/PartialView.cshtml", model);
    return Json(result, JsonRequestBehavior.DenyGet);
}

Hope this helps.

Thursday, May 22, 2014

SQLite: How to Enforce Foreign Key Constraints

Enabling SQLite Foreign Keys:

SQLite lets create foreign keys but will not enforce them upon insertion or deletion. I spent sometime playing with this and it turns out that there are two ways to make SQLite enforce the foreign key constraint:

1) And the this is the easiest way. In the connection string you can specify that foreign keys should be enforce like so (tested using System.Data.SQLite ADO.NET driver):
<add name="ConnectionName" connectionString="Data Source=dbname.db;Version=3;New=True;Foreign Keys=True;" providerName="System.Data.SQLite"/>
2) For every connection you can turn on the foreign_keys pragama using the command
PRAGMA foreign_keys = ON; as documented here http://www.sqlite.org/foreignkeys.html#fk_enable

So with the second approach one can do something similar to the following pseudocode:

SQLiteConnection cn = new SQLiteConnection(...);
SQLiteCommend cmd = new SQLiteCommand(cn);
cn.Open();
cmd.CommandText = "PRAGMA foreign_keys = ON;"
cmd.ExecuteNonQuery(); /* so far we ensured that foreign keys constraints will be enforced */
cmd.CommandText = /* some SQL code here */
cmd.Execute ...

I prefer approach 1).